|
Author |
Topic: sorting objects by property (Read 850 times) |
|
toxi
|
sorting objects by property
« on: May 10th, 2004, 4:46pm » |
|
for my current work project i need to sort arrays of objects by a specific property value, something the default sort() function unfortunately can't do. so below is a "little santa's helper" (and a tiny demo) you can use/misuse to do just that in your own sketches... Code:// quick sort util class // based on the standard implementation in Bagel class SortUtil { Object[] items; SortUtil(Object[] s) { items=s; } void sort() { this.sort(0,items.length-1); } void sort(int i, int j) { int pivotIndex = (i+j)/2; swap(pivotIndex, j); int k = subsort(i-1, j); swap(k, j); if ((k-i) > 1) sort(i,k-1); if ((j-k) > 1) sort(k+1,j); } int subsort(int left, int right) { int pivot = right; do { while (compare(++left, pivot) < 0); while ((right != 0) && (compare(--right, pivot) > 0)); swap(left, right); } while (left < right); swap(left, right); return left; } void swap(int a, int b) { Object temp = items[a]; items[a] = items[b]; items[b] = temp; } // this method needs to be overwritten // in the derived classes // needs to return the following: // -1 - if A < B // 0 - if A = B // 1 - if A > B int compare(int a, int b) { return 0; } } // a little data type for demo purposes below class Dummy { int val; String letter; Dummy(int xx, String l) { val=xx; letter=l; } } // custom version of the SortUtil class // to sort objects of the class "Dummy" // by their "letter" property class DummySortByLetter extends SortUtil { // constructor needs to call the generic one // in the super class SortUtil DummySortByLetter(Dummy[] e) { super((Object[])e); } // custom version of compare method //to check 2 values (here 2 strings) int compare(int a, int b) { Dummy objA=(Dummy)items[a]; Dummy objB=(Dummy)items[b]; return objA.letter.compareTo(objB.letter); } } // another custom version of the SortUtil class // to sort objects of the class "Dummy" // by their "value" property class DummySortByValue extends SortUtil { DummySortByValue(Dummy[] e) { super((Object[])e); } // here we compare the "value" integer // properties of the objects int compare(int a, int b) { Dummy objA=(Dummy)items[a]; Dummy objB=(Dummy)items[b]; if (objA.val < objB.val) return -1; return (objA.val == objB.val) ? 0 : 1; } } void setup() { Dummy[] dummies=new Dummy[4]; dummies[0]=new Dummy(200,"o"); dummies[1]=new Dummy(100,"i"); dummies[2]=new Dummy(-50,"x"); dummies[3]=new Dummy(20,"t"); // demonstrate alphabetical sorting of the objects, // using the "letter" property println("sort by letter..."); DummySortByLetter sortByLetter=new DummySortByLetter(dummies); sortByLetter.sort(); for(int i=0; i<dummies.length; i++) { println(dummies[i].letter+" "+dummies[i].val); } // demonstrate numerical sorting, // using the int property values println("sort by value..."); DummySortByValue sortByVal=new DummySortByValue(dummies); sortByVal.sort(); for(int i=0; i<dummies.length; i++) { println(dummies[i].letter+" "+dummies[i].val); } } |
|
|
http://toxi.co.uk/
|
|
|
TomC
|
Re: sorting objects by property
« Reply #1 on: May 10th, 2004, 6:41pm » |
|
Here's a similar thing from my archives. Implementing the Comparable interface from Java, and using Arrays.sort() Code: class MyThing implements Comparable { int value = 0; // return 0 if equal, < 0 if less, > 0 if greater public int compareTo(Object other) { return this.value - ((MyThing)other).value; } public String toString() { return String.valueOf(value); } } MyThing[] stuff; void setup() { stuff = new MyThing[12]; println("before sorting:"); for (int i = 0; i < stuff.length; i++) { stuff[i] = new MyThing(); stuff[i].value = (int)random(stuff.length); println(stuff[i]); } Arrays.sort(stuff); println("after sorting:"); for (int i = 0; i < stuff.length; i++) { println(stuff[i]); } } |
|
|
« Last Edit: May 10th, 2004, 6:42pm by TomC » |
|
|
|
|
|