1

I want to sort a vector which contains Object of following fields

public class CategoryListing   {

     String company_id;
     String company_name;
     double distance;

     //setters and getters here 
}

I populated the vector from webservice, now I want to sort that vector by distance, means I want to show nearest one. How can I sort the vector which contains objects?

gnat
  • 6,213
  • 108
  • 53
  • 73
Ali
  • 10,774
  • 10
  • 56
  • 83
  • 1
    http://stackoverflow.com/questions/5113006/sort-a-vector-of-custom-objects Or if you use a list: http://stackoverflow.com/questions/2784514/sort-arraylist-of-custom-objects-by-property – assylias Oct 11 '12 at 07:13
  • 4
    It is not the same Question. Black Berry only supports Java 1.3 Limited functionality. There is no `Collections` class. I think question needs to be reopened. – Amit Deshpande Oct 11 '12 at 09:17

2 Answers2

4

None of the other answers are going to work for you on Blackberry as it doesn't support Generics. Blackberry uses older version of Java. Try the following code. It uses SimpleSortingVector

    SimpleSortingVector sortVector = new SimpleSortingVector();
    sortVector.setSortComparator(new Comparator() {

        public int compare(Object o1, Object o2) {

            CategoryListing o1C = (CategoryListing)o1;
            CategoryListing o2C = (CategoryListing)o2;
            return Double.toString((o1C.distance)).compareTo(Double.toString(o2C.distance));
        }
    });
            //when you add elements to this vector, it is automatically sorted by distance
    sortVector.addElement(new CategoryListing());
Mark Loeser
  • 17,657
  • 2
  • 26
  • 34
rfsk2010
  • 8,571
  • 4
  • 32
  • 46
2

You can use Comparator.

Define a comparator which will sort on distance like below

public class DistanceComparator implements Comparator {
    public int compare(Object o1, Object o2) {
        return Double.valueOf(((CategoryListing) o1).distance)
                .compareTo(((CategoryListing) o2).distance);// use getters
    }
}

Sort vector using below sort method. Note As Arrays are present from 1.2 It is also present for blackberry.

public static void sort(Vector vector,Comparator comparator) {

    Object[] array = new Object[vector.size()];   
    vector.copyInto(array);   

    Arrays.sort(array,comparator);

    int i = 0;
    Enumeration enumumeration = vector.elements();
    while (enumumeration.hasMoreElements()) {
        enumumeration.nextElement();
        vector.insertElementAt(array[i++], i);
    }
}
Amr Angry
  • 3,711
  • 1
  • 45
  • 37
Amit Deshpande
  • 19,001
  • 4
  • 46
  • 72
  • 1
    Collections is not availbale in blackberry sdk – Ali Oct 11 '12 at 07:32
  • 1
    Correction to previous post - Generics aren't supported in BlackBerry as well as annotations. It's Supports limited 1.3 only – Eugen Martynov Oct 11 '12 at 07:34
  • i am not able to find collection class so i can not implement it – Ali Oct 11 '12 at 07:48
  • 1
    Amit its not my negative you are helping me how i can provide negative, – Ali Oct 11 '12 at 08:05
  • @Ali Above code will work for you. Try it let me know if there are any problems. – Amit Deshpande Oct 11 '12 at 08:07
  • ListIterator is not available in blackberry – Ali Oct 11 '12 at 09:07
  • Errr. Life would have been difficult to work on BB. Check the updated solution. I have checked it has `Enumeration` so you can use that. – Amit Deshpande Oct 11 '12 at 09:16
  • 2
    or use my solution using SimpleSortingVector :-) .There is no need to convert vector to array, then sort it and then set the vector again. too much code and calculations which are not required. – rfsk2010 Oct 11 '12 at 10:07
  • -1 in BB 7 API, there is no `java.util.Comparator`, see **[Blackberry API javadocs](http://www.blackberry.com/developers/docs/7.1.0api/java/util/package-frame.html)** – gnat Oct 14 '12 at 05:27
  • @gnat if you would have done `Frames` you would have found it here is the [link](http://www.blackberry.com/developers/docs/7.1.0api/com/google/zxing/common/Comparator.html) from your link only – Amit Deshpande Oct 14 '12 at 05:34
  • 1
    @AmitD I see, you're right. Reverted my vote from down to up :) – gnat Oct 14 '12 at 05:39