Puzzle Sort List

Assuming I have

final Iterable<String> unsorted = asList("FOO", "BAR", "PREFA", "ZOO", "PREFZ", "PREFOO");

What can I do to convert this unsorted list to this:

[PREFZ, PREFA, BAR, FOO, PREFOO, ZOO]

(a list starting with known values ​​that should appear first (here "PREFA" and "PREFZ"), and the rest are sorted alphabetically)

I think there are several useful classes in guava that can do the job (Ordering, Predicates ...), but I haven't found a solution yet ...

+3
source share
6 answers

You specifically mentioned guava; along with Sylvan M the answer, here is another way (more like an academic exercise and demonstrating the flexibility of a guava than anything else)

// List is not efficient here; for large problems, something like SkipList 
// is more suitable
private static final List<String> KNOWN_INDEXES = asList("PREFZ", "PREFA");

private static final Function<Object, Integer> POSITION_IN_KNOWN_INDEXES 
    = new Function<Object, Integer>() {
  public Integer apply(Object in) {
     int index = KNOWN_INDEXES.indexOf(in);
     return index == -1 ? null : index;
  }     
};


...


List<String> values = asList("FOO", "BAR", "PREFA", "ZOO", "PREFZ", "PREFOO");

Collections.sort(values,
  Ordering.natural().nullsLast().onResultOf(POSITION_IN_KNOWN_INDEXES).compound(Ordering.natural())
);

, , Integer, List.indexOf(), .

, , .

+1

.

. , , .

knownUnsorted.addAll(unsorted.size - 1, unknonwUnsorted);
+3

List Collections.sort(...).

-

Collections.sort(myList, new FunkyComparator());

:

class FunkyComparator implements Comparator {

    private static Map<String,Integer> orderedExceptions =
        new HashMap<String,Integer>(){{ 
            put("PREFZ", Integer.valueOf(1));
            put("PREFA", Integer.valueOf(2));
        }};

    public int compare(Object o1, Object o2) {
        String s1 = (String) o1;
        String s2 = (String) o2;
        Integer i1 = orderedExceptions.get(s1);
        Integer i2 = orderedExceptions.get(s2);

        if (i1 != null && i2 != null) {
            return i1 - i2;
        }
        if (i1 != null) {
            return -1;
        }
        if (i2 != null) {
            return +1;
        }
        return s1.compareTo(s2);
    }
}
+3

. . , , .

Collections.sort(list) .

.

String special = "PREFA";
if (list.remove(special)
    list.add(0, special);

, , , :

String[] knownValues = {};
for (String s: knownValues) {
    if (list.remove(s))
        list.add(0, s);
}
+2

guava lib, , . , , , , :

final Iterable<String> all = asList("FOO", "BAR", "PREFA", "ZOO", "PREFOO", "PREFZ");
final List<String> mustAppearFirst = asList("PREFZ", "PREFA");
final Iterable<String> sorted = 
      concat(
            Ordering.explicit(mustAppearFirst).sortedCopy(filter(all, in(mustAppearFirst))),
            Ordering.<String>natural().sortedCopy(filter(all, not(in(mustAppearFirst)))));
+2

Collections.sort(list), , , , .

class MyComparator implements Comparator<String> {

    public int compare(String o1, String o2) {
        // Now you can define the behaviour for your sorting.
        // For example your special cases should always come first, 
        // but if it is not a special case then just use the normal string comparison.

        if (o1.equals(SPECIAL_CASE)) {
            // Do something special
        }
        // etc.
        return o1.compareTo(o2);
    }

}

:

Collections.sort(list, new MyComparator());
+1

Source: https://habr.com/ru/post/1751571/


All Articles