Skip to content

Instantly share code, notes, and snippets.

Created December 4, 2013 23:18
Show Gist options
  • Save anonymous/7797380 to your computer and use it in GitHub Desktop.
Save anonymous/7797380 to your computer and use it in GitHub Desktop.
preprocessing java ftw
$ gcc -E -P Test.java.c > Test.java
$ javac Test.java
$ java Test
Bar ist 3 Jahre alt
Foo ist 5 Jahre alt
Qux ist 6 Jahre alt
#define SORT(list,T,code) java.util.Arrays.sort(list, new java.util.Comparator<T>() { public int compare(T a, T b) { code } })
class Person {
String name;
int age;
Person(String name, int age) { this.name=name; this.age=age; }
}
class Test {
public static void main(String[] args) {
Person[] personen = new Person[]{new Person("Foo", 5), new Person("Bar", 3), new Person("Qux", 6)};
// sort by age
SORT(personen,Person, {
if (a.age < b.age) return -1;
if (a.age > b.age) return 1;
return 0;
});
for (Person p: personen) System.out.println(p.name+" ist "+p.age+" Jahre alt");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment