Saturday, 2 March 2013

Sort array in Android and iOS

For iOS, use


// Sort by timing
direction.timings = [[direction.timings sortedArrayUsingComparator:(NSComparator)^(NSString* a, NSString* b){
      return ([a longLongValue] > [b longLongValue]);
}] mutableCopy];


for android, use


     // sort by raw timing
    Collections.sort(items, new Comparator<Bus>() {
        public int compare(Bus a, Bus b) {
            return Long.signum(fixString(a.timing) - fixString(b.timing));
        }
        private long fixString(String in) {
            return Long.parseLong(in); // .substring(0, in.indexOf(' ')));
        }
    });

No comments:

Post a Comment