Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort an Object[] array based on one of its elements?

Tags:

java

I need your assiatance please:

I have a map :

Map<String, Object[]> data = new HashMap<String, Object[]>();

The Object[] array is built out of triples: [String, String, Integer]

Later on, I am going to read the object[] array values, but I should print them sorted according to the integer value.

How can I do this in the best way?

Thank you!

like image 823
dushkin Avatar asked Jan 26 '26 18:01

dushkin


1 Answers

One way is with a stream:

data.values().stream()
    .sorted(Comparator.comparing(a -> (Integer)a[2]))
    .forEachOrdered(a -> System.out.println(Arrays.toString(a)));
like image 191
Alex - GlassEditor.com Avatar answered Jan 28 '26 10:01

Alex - GlassEditor.com



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!