Skip to content

Commit 63c6622

Browse files
committed
Add object methods support for the sortBy(), groupBy(), countBy() and
indexBy().
1 parent 69bac5b commit 63c6622

File tree

3 files changed

+62
-4
lines changed

3 files changed

+62
-4
lines changed

src/main/java/com/github/underscore/$.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -504,8 +504,8 @@ public static <E> Set<E> sample(final List<E> list, final int howMany) {
504504
return samples;
505505
}
506506

507-
public static <E, T extends Comparable<? super T>> List<E> sortBy(final List<E> list, final Function1<E, T> func) {
508-
final List<E> sortedList = newArrayList(list);
507+
public static <E, T extends Comparable<? super T>> List<E> sortBy(final Iterable<E> iterable, final Function1<E, T> func) {
508+
final List<E> sortedList = newArrayList(iterable);
509509
Collections.sort(sortedList, new Comparator<E>() {
510510
@Override
511511
public int compare(E o1, E o2) {
@@ -515,8 +515,12 @@ public int compare(E o1, E o2) {
515515
return sortedList;
516516
}
517517

518-
public static <K, V extends Comparable<? super V>> List<Map<K, V>> sortBy(final List<Map<K, V>> list, final K key) {
519-
final List<Map<K, V>> sortedList = newArrayList(list);
518+
public <E, T extends Comparable<? super T>> List<E> sortBy(final Function1<E, T> func) {
519+
return sortBy((Iterable<E>) iterable, func);
520+
}
521+
522+
public static <K, V extends Comparable<? super V>> List<Map<K, V>> sortBy(final Iterable<Map<K, V>> iterable, final K key) {
523+
final List<Map<K, V>> sortedList = newArrayList(iterable);
520524
Collections.sort(sortedList, new Comparator<Map<K, V>>() {
521525
@Override
522526
public int compare(Map<K, V> o1, Map<K, V> o2) {
@@ -542,6 +546,10 @@ public static <K, E> Map<K, List<E>> groupBy(final Iterable<E> iterable, final F
542546
return retVal;
543547
}
544548

549+
public <K, E> Map<K, List<E>> groupBy(final Function1<E, K> func) {
550+
return groupBy((Iterable<E>) iterable, func);
551+
}
552+
545553
public static <K, E> Map<K, List<E>> indexBy(final Iterable<E> iterable, final String property) {
546554
return groupBy(iterable, new Function1<E, K>() {
547555
@Override
@@ -555,6 +563,10 @@ public K apply(E elem) {
555563
});
556564
}
557565

566+
public <K, E> Map<K, List<E>> indexBy(final String property) {
567+
return indexBy((Iterable<E>) iterable, property);
568+
}
569+
558570
public static <K, E> Map<K, Integer> countBy(final Iterable<E> iterable, Function1<E, K> func) {
559571
final Map<K, Integer> retVal = newLinkedHashMap();
560572
for (E e : iterable) {
@@ -568,6 +580,10 @@ public static <K, E> Map<K, Integer> countBy(final Iterable<E> iterable, Functio
568580
return retVal;
569581
}
570582

583+
public <K, E> Map<K, Integer> countBy(Function1<E, K> func) {
584+
return countBy((Iterable<E>) iterable, func);
585+
}
586+
571587
public static <E> E[] toArray(final Iterable<E> iterable) {
572588
return (E[]) newArrayList(iterable).toArray();
573589
}

src/test/java/com/github/underscore/CollectionsTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,14 @@ public Integer apply(Integer item) {
771771
}
772772
});
773773
assertEquals("[5, 4, 6, 3, 1, 2]", result.toString());
774+
final List<Integer> resultObj =
775+
new $(asList(1, 2, 3, 4, 5, 6)).sortBy(
776+
new Function1<Integer, Integer>() {
777+
public Integer apply(Integer item) {
778+
return Double.valueOf(Math.sin(item) * 1000).intValue();
779+
}
780+
});
781+
assertEquals("[5, 4, 6, 3, 1, 2]", resultObj.toString());
774782
}
775783

776784
/*
@@ -808,6 +816,14 @@ public Double apply(Double num) {
808816
}
809817
});
810818
assertEquals("{1.0=[1.3], 2.0=[2.1, 2.4]}", result.toString());
819+
final Map<Double, List<Double>> resultObj =
820+
new $(asList(1.3, 2.1, 2.4)).groupBy(
821+
new Function1<Double, Double>() {
822+
public Double apply(Double num) {
823+
return Math.floor(num);
824+
}
825+
});
826+
assertEquals("{1.0=[1.3], 2.0=[2.1, 2.4]}", resultObj.toString());
811827
}
812828

813829
/*
@@ -835,6 +851,9 @@ public String toString() {
835851
final Map<String, List<Person>> result =
836852
$.indexBy(asList(new Person("moe", 40), new Person("larry", 50), new Person("curly", 60)), "age");
837853
assertEquals("{40=[moe, 40], 50=[larry, 50], 60=[curly, 60]}", result.toString());
854+
final Map<String, List<Person>> resultObj =
855+
new $(asList(new Person("moe", 40), new Person("larry", 50), new Person("curly", 60))).indexBy("age");
856+
assertEquals("{40=[moe, 40], 50=[larry, 50], 60=[curly, 60]}", resultObj.toString());
838857
final Map<String, List<Person>> result2 =
839858
$.indexBy(asList(new Person("moe", 40), new Person("larry", 50), new Person("curly", 60)), "age2");
840859
assertEquals("{null=[moe, 40, larry, 50, curly, 60]}", result2.toString());
@@ -866,6 +885,14 @@ public String apply(Person person) {
866885
}
867886
});
868887
assertEquals("{moe=2, curly=1}", result.toString());
888+
final Map<String, Integer> resultObj =
889+
new $(asList(new Person("moe", 40), new Person("moe", 50), new Person("curly", 60))).countBy(
890+
new Function1<Person, String>() {
891+
public String apply(Person person) {
892+
return person.name;
893+
}
894+
});
895+
assertEquals("{moe=2, curly=1}", resultObj.toString());
869896
}
870897

871898
/*

src/test/java/com/github/underscore/UnderscoreTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,4 +220,19 @@ public void optional() {
220220
assertEquals("Optional.absent()", Optional.absent().toString());
221221
assertEquals("Optional.of(1)", Optional.of(1).toString());
222222
}
223+
224+
@Test
225+
public void stackoverflow() {
226+
// http://stackoverflow.com/questions/109383/how-to-sort-a-mapkey-value-on-the-values-in-java?rq=1
227+
assertEquals("[D=67.3, B=67.4, C=67.4, A=99.5]", $.sortBy((new LinkedHashMap<String, Double>() { {
228+
put("A", 99.5);
229+
put("B", 67.4);
230+
put("C", 67.4);
231+
put("D", 67.3);
232+
} }).entrySet(), new Function1<Map.Entry<String, Double>, Double>() {
233+
public Double apply(Map.Entry<String, Double> item) {
234+
return item.getValue();
235+
}
236+
}).toString());
237+
}
223238
}

0 commit comments

Comments
 (0)