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 Array[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
12
Views
3 Years
Discussion Span
Last Post by jwenting
0

I don't know of any simple solution. You may have to create a tiny class with the data value and its original index as its two instance variables. Then you can sort an array of those objects on the data value, but still retrieve their original indexes.

0

The only ways would be to either make a class containing the index and value, as James suggests, to create a separate array containing the indices of the values in sorted order, or to create a new array of the values in sorted order.

The very concept is weird btw, if you're going to sort it you're no longer interested in the original order so the original index should be of no consequence.

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.