Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

How do I sort a vector / array in Java?

Featured Replies

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);
}

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 :)

  • 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 by nickyoung

  • 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);
}

  • Author

double post :)

Edited by nickyoung

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.