int a,b;
                            double temp;
                            int sortTheNumbers = sentenceList.size() - 1;
                            for (a = 0; a < sortTheNumbers; ++a)
                            {
                            for (b = 0; b < sortTheNumbers; ++b)
                            if(score[b] < score[b + 1])
                            {
                            temp = score[b];
                            score[b] = score[b + 1];
                            score[b + 1] = temp;
                            }

                            }

from the code above, i have managed to sort my array in descending order.
but i have a problem in modifying the code to remember the index of the score.

the output should be something like:

System.out.println(Index:"+index+" with the score"+score);

Index 3 with score 7
Index 0 with score 4
Index 2 with score 3
Index 4 with score 2
Index 1 with score 0

Anyone help me please >.< Have been hard-coding 3-4 hours nonstop for my FYP(mind became blank?) >.<

Recommended Answers

All 11 Replies

or is there any simpler ways using

import java.util.Arrays;

using the sort function?

i have a problem in modifying the code to remember the index of the score.

Can you explain which score you want to remember the index to?

Can you explain which score you want to remember the index to?

i'm greedy actually...coz i need all the index of each scores
currently doing text summarization, somehow need to remember all the index of the highest to the lowest score
my englist is...sorry if this is not clear...i've been coding like 6 hours now (on other parts too) :confused:

int [] score;
score = new int[5];

score[0] = 4;
score[1] = 0;
score[2] = 3;
score[3] = 7;
score[4] = 2;

let say the above code is the input, how may i produce:
Index 3 with score 7
Index 0 with score 4
Index 2 with score 3
Index 4 with score 2
Index 1 with score 0

i hope this will makes thing clearer >.<

How will you know which index points to what score?

If the array is sorted in ascending order, then the first index (0) refers to the lowest score and the last index(array size -1) refers to the highest score.

About your last post.
I don't see what your problem is. The index values you show point to the elements you want them to point to.
For example:
Index 3 with score 7
The element at score[3] is a 7

Is this what you want?
An array with the values: 3,0,2,4,1
and to leave the contents of the original array unchanged.

To do this you need to create the new array, fill it with the numbers 0 to the array size -1 and then use the elements of that array as the index to the original array that you don't want to change. You'll swap the elements in this array vs those in the original array.
It gets a bit tricky. You should use a paper and pencil to work through the logic before trying to code it.

How will you know which index points to what score?

If the array is sorted in ascending order, then the first index (0) refers to the lowest score and the last index(array size -1) refers to the highest score.

to my code above, score will become:
score[0] = 7;
score[1] = 4;
score[2] = 3;
score[3] = 2;
score[4] = 0;

let say i would want to make another array...called position []

i would like the output of position as follows:
position[0] = 3;
position[1] = 0;
position[2] = 2;
position[3] = 4;
position[4] = 1;

Our posts crossed. Please go back and read my last.

sry...any tips?
at every iteration, the the positioning wont be the same as the initial...i end up storing positions by the iterations

i got it...make 3 arrays
1 array is the original..1 when sorted
compare both to make the position array
swt...im out of fume...thinking all complicated...zzzzzz

Yes it is complicated.
What is the third array for?
One is the original
One is the indexes to the original
The third is for ???

one is for the original(non sorted)
2nd is for the sorted
third is the original index of the sorted..done when comparing those other two arrays^^

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.