I would like to sort two arrays according to their numeric values.For eg
apple[] = [apples, green, 9180, 16.19];
orange[] = [oranges, orange color, 9180, 25.18];
I would like to sort it according to the 3rd value in the array i.e 16.19
How will I go about this ?
Note array declaration is for illustrative purposes only.
The result must look like this:
String[] apples = [apples, green, 9180, 16.19];
String[] oranges = [oranges, orange color, 9180, 25.18];
String[] grapes = [grapes, green, 9180, 35.16]
String[] strawberry = [strawberries, red, 9180, 45.36]
Each line is a single separate array. It must be sorted in ascending order according to the last number i.e 16.19, 25.18, 35.16
The array has four fields [apples, green, 9180, 16.19]; Apples: name of the fruit; Green: color of the fruit; 9180: pin code of farm; 16.19: yield of fruits. I must sort these according to the yield of fruits. Hope I am clear.
I allowed myself the assumption that you don’t have four variables named
oranges,apples,strawberriesandgrapes. If you do, put them inside an array before sorting.Example code to sort an array of arrays of
Comparables:Output:
If you want something even more generic, you can define an array comparator which takes the comparison column as a parameter in its constructor (see updated example):