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

Recommended Answers

All 2 Replies

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.

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.

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.