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

Recommended Answers

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 …

Jump to Post

All 2 Replies

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.

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.

Be a part of the DaniWeb community

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