0

I have an ArrayList of ArrayList of Floats and I'm looking to sort the array by one of the values of the inner ArrayList.

For example

[1.2,2.0,3.5]
[1.3,3.3,5.6]
[5.7,5.3,1.4]

I want to sort this by the last value so that the output will be as the following (lowest to highest)

[5.7,5.3,1.4]
[1.2,2.0,3.5]
[1.3,3.3,5.6]

The values themselves do not change just the order so that the ArrayList goes from lowest to highest based on the 3rd value in the inner ArrayList.

I tried list.sort(Comparator.comparing(l -> l.get(2))); as suggested below but I am getting an error: The method comparing(( l) -> {}) is undefined for the type Comparator

huddie96
  • 1,240
  • 2
  • 13
  • 26
  • 1
    You can pass your custom `Comparator` to the [Collections.sort](https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#sort(java.util.List,%20java.util.Comparator)) – user3707125 Aug 28 '16 at 20:53
  • Sorting list of lists is same as sorting list of any other objects based on property of that object. In your case property is third element in list. – Pshemo Aug 28 '16 at 20:57
  • 2
    This is easily done with: `list.sort(Comparator.comparing(l -> l.get(2)));` – Jorn Vernee Aug 28 '16 at 20:58
  • @JornVernee Sorry I'm new to Java and don't understand. Do you mind elaborating? – huddie96 Aug 28 '16 at 21:07
  • @huddie96 Did you visit duplicate question (you may need to refresh this page to see it at top of your question)? – Pshemo Aug 28 '16 at 21:08
  • @Pshemo I just saw it, the generic bean one seems to be what im looking for but as I said I'm very new and since that deals with strings I believe I have no idea how to convert it.-- I tried though – huddie96 Aug 28 '16 at 21:11
  • @huddie96 Elaborate what exactly? I gave you the code to do what you want, (assuming your list is named `list`). What don't you understand? – Jorn Vernee Aug 28 '16 at 21:11
  • I took exactly what you said but it generated an error. This works with an ArrayList of floats? – huddie96 Aug 28 '16 at 21:14
  • @huddie96 It works for `List>`, so it would also work for `ArrayList>`. – Jorn Vernee Aug 28 '16 at 21:15
  • Jorn's solution works fine for me. What error did you get exactly? – Pshemo Aug 28 '16 at 21:16
  • Also maybe this answer will help you understand Jorn's answer better https://stackoverflow.com/questions/1814095/sorting-an-arraylist-of-contacts-based-on-name – Pshemo Aug 28 '16 at 21:17
  • @JornVernee What does the l represent in (l -> l.get(2)..? – huddie96 Aug 28 '16 at 23:57
  • @Pshemo Heres the error: The method comparing(( l) -> {}) is undefined for the type Comparator – huddie96 Aug 29 '16 at 00:25
  • I have a mac and I'm running Java 6 – huddie96 Aug 29 '16 at 00:32
  • @huddie96 `l` is the inner list. You need at least Java 8 to use it like that though. Are you by any chance using a raw type for the outer list? – Jorn Vernee Aug 29 '16 at 05:30
  • @JornVernee My outer list is created as so: public static ArrayList> Levels = new ArrayList>(); -- When i switched the project to 1.8 I get many errors – huddie96 Aug 29 '16 at 13:01
  • Okay upgraded and it works. Your answer is so easy (once your running Java 8) you should add it to the original question (since this is a duplicate) – huddie96 Aug 29 '16 at 14:37

0 Answers0