February 23, 201115 yr Hi Guys, I am trying to sort a vector/array in Java to make a Blackberry Application. Other forums talk about the use of a collection object to sort but I don't have this object in my library/JRE 5.0....or at least I can't find it. Here is a sort function I have attempted to create but it doesn't exactly work correctly...it outputs duplicates and not in alpha ASC order... public Vector sortBusinessesVectorASC(Vector businesses){ Vector sortedBusinesses = new Vector(); sortedBusinesses.setSize(businesses.size()); for(int i=0; i<businesses.size()-1;i++){ for (int j=0;j<businesses.size()-1;j++){ JSONObject business = (JSONObject) businesses.elementAt(j); JSONObject nextBusiness = (JSONObject) businesses.elementAt(j+1); try{ if(business.getString("name").compareTo(nextBusiness.getString("name")) < 0){ //phonebook.log(business.getString("name")+" < "+nextBusiness.getString("name")); sortedBusinesses.setElementAt(nextBusiness.getString("name"), j+1); sortedBusinesses.setElementAt(business.getString("name"), j); }else{ sortedBusinesses.setElementAt(nextBusiness.getString("name"), j); sortedBusinesses.setElementAt(business.getString("name"), j+1); } }catch(Exception e){ phonebook.error(e); } } } phonebook.log(sortedBusinesses.size()+""); return(sortedBusinesses); }
February 24, 201115 yr The easiest way is to implement a Comparator class, then use the java.util.Collections.sort method (which you can do, since Vector implements List). See the java.util.Collections javadoc. This assumes you have the Collections class and the sort(List,Comparator) method on your platform - I'm not sure what parts of the Java API are available for Blackberry apps. Have a go at understanding the sort/Comparator method, and post back if you get stuck
February 24, 201115 yr Author Thanks Connetu, the part about creating a Comparator was very helpful. Here's what I found out so far for Blackberry Apps... Collections.sort not working. Instead use a similar function - Arrays.sort Here is what it looks like....still testing this out. * Note on Data Types - Make sure to use an Object Array for the first argument in the Arrays.sort method. So if you have your data in a hashtable or vector, you'll wanna convert to an object array - see post below to do this... import net.rim.device.api.util.Arrays; import net.rim.device.api.util.Comparator; class sortStringAscendingComparator implements Comparator{ public int compare(Object o1, Object o2) { String person = (String) o1; String nextPerson = (String) o2; if(person.compareTo(nextPerson)<0){ return(-1); } if(person.compareTo(nextPerson)>0){ return(1); } return(0); } } Object [] people = new Object[3] people[0] = new String("person 0"); people[1] = new String("person 1"); people[2] = new String("person 2"); Arrays.sort(people, new sortStringAscendingComparator()); Edited February 24, 201115 yr by nickyoung
February 24, 201115 yr Author Convert hashtable to object array public Object hashTableToObjectArray(Hashtable ht){ Object[] objectArray = new Object[ht.size()]; int i = 0; Enumeration htElements = ht.elements(); while(htElements.hasMoreElements()){ objectArray[i] = htElements.nextElement(); i++; } return(objectArray); } Convert vector to object array public Object vectorToObjectArray(Vector vector){ Object[] objectArray = new Object[vector.size()]; int i = 0; Enumeration elements = vector.elements(); while(elements.hasMoreElements()){ objectArray[i] = elements.nextElement(); i++; } return(objectArray); }
Create an account or sign in to comment