I\'m learning and understanding Java now, and while practising with arrays I had a doubt. I wrote the following code as an example:
Some people propose making a product type. That is feasible only if the amount of elements is small. By introducing another object you add object overhead (30+ bytes) for each element and a performance penalty of a pointer (also worsening cache locality).
Make a third array. Fill it with indices from 0
to size-1
. Sort this array with comparator function polling into the array according to which you want to sort.
Finally, reorder the elements in both arrays according to indices.
Write the sorting algorithm yourself. This is not ideal, because you might make a mistake and the sorting efficiency might be subpar.
The arrays are not linked in any way. Like someone pointed out take a look at
SortedMap
http://docs.oracle.com/javase/7/docs/api/java/util/SortedMap.html
TreeMap
http://docs.oracle.com/javase/7/docs/api/java/util/TreeMap.html