0

Hi everyone
I have a question and I need your help
I want to sort an array but I dont want to change the index
I mean if I have an array F[6,5,4,3,2,1] it will be like this
0 6
1 5
2 4
3 3
4 2
5 1
and after i sort it, it will be like this
0 1
1 2
2 3
3 4
4 5
5 6
but I want to sort it and keep the index with the value
5 1
4 2
3 3
2 4
1 5
0 6
please let me know if there is any function to sort the array as i wrote above

ur help will be greatly appreciated

3
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by bibiki
0

Create an object with two instance fields. One for the original index and one for the value. Have it implement Comparable and sort according to the value. Sorting, of course, will give the objects a new index within the array, but they will, at least, still have an instance field indicating what the original index was.

0

another way I could think of doing this is building a two dimensional array of 6*2 length. on one column, you could save the indexes, and on the other, save the actual numbers you want to sort. then sort the array in the column where you have your actual values saved. hope this helps.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.