2

I got an ArrayList which contains next data : coordX (int) , coordY (int) , length (int), position (String).I sort this array by the length field. How can I give priority if the lengths are equal by the position filed (priority will have that length that has horizontal position). For example, given the input:

coordX = 1  ,  coordY = 0   , length = 2  ,  position  - vertical
coordX = 5  ,  coordY = 3   , length = 2  ,  position  - horizontal
coordX = 1  ,  coordY = 6   , length = 4  ,  position  - horizontal
coordX = 3  ,  coordY = 6   , length = 1  ,  position  - "1x1"

Output should be :

coordX = 1  ,  coordY = 6   , length = 4  ,  position  - horizontal
coordX = 5  ,  coordY = 3   , length = 2  ,  position  - horizontal
coordX = 1  ,  coordY = 0   , length = 2  ,  position  - vertical
coordX = 3  ,  coordY = 6   , length = 1  ,  position  - "1x1"

Here is what I have :

    public static void insertionSort(ArrayList<sort> srt) {
    int i,j;
    for (i = 1; i < srt.size(); i++) {
        sort tmp = srt.get(i);
        j = i;
        while ((j > 0) && (srt.get(j - 1).length > tmp.length)) {
            srt.set(j, srt.get(j - 1));
            j--;
        }
        srt.set(j, tmp);
    }
}
Alessandro
  • 35
  • 5
  • 2
    First, there is [`Collections.sort()`](https://download.java.net/java/GA/jdk14/docs/api/java.base/java/util/Collections.html#sort%28java.util.List,java.util.Comparator%29). With that, it would look like: `Collections.sort(list, Comparator.comparingInt(s -> s.length).thenComparing(s -> s.position))`. – Johannes Kuhn Apr 02 '20 at 14:04
  • You should use `Collections.sort` instead and write a Comparator or have your class implement Comparable – Joakim Danielson Apr 02 '20 at 14:04
  • Does this answer your question? [Javascript sort array by two fields](https://stackoverflow.com/questions/6129952/javascript-sort-array-by-two-fields) – Serhii Povísenko Apr 02 '20 at 14:40
  • @povisenko This question is about Java, linking to a duplicate for Javascript is not correct. – Mark Rotteveel Apr 03 '20 at 14:06

1 Answers1

1
        List<YourClass> list = new ArrayList<>();

The following works like this.

  • Comparator.comparing(YourClass::getLength).reversed() sorts on the length field in desending order.
  • if the above results in equal lengths, then .thenComparing(YourClass::getPosition) sorts on the position in lexical order.
        list.sort(Comparator.comparing(YourClass::getLength).reversed()
                .thenComparing(YourClass::getPosition));
        // or with no getters

        list.sort(Comparator.comparing((YourClass cl) -> cl.length).reversed()
                .thenComparing((YourClass cl) -> cl.position));

A possible structure of your coordinate class.

class YourClass {
    int length;
    public String position;
    int coordX;
    int coordY;

    public int getLength() {
        return length;
    }

    public String getPosition() {
        return position;
    }

    public int getCoordX() {
        return coordX;
    }

    public int getCoordY() {
        return coordY;
    }
}

WJS
  • 36,363
  • 4
  • 24
  • 39