This is not a question just small challenge if you interest in improving provided solution and feedback for me.

I just went back to some assignment I did at university and one of these been design phone book for mobile device with use of Java Microedition (JME). At the time I had no time to play around with certain functionalities and this included contact sorting. Initial contact list display only contact name and first number associated with the contact, plus ID from Record Management Store (RMS) to for easier contact look up.
These contacts are kept in ContactListLabels

public class ContactListLabel {

    private int contactID;
    private String nameLabel;
    private String numberLabel;

    public ContactListLabel(){}

    public ContactListLabel(int id, String name, String number){
        setContactID(id);
        setNameLabel(name);
        setNumberLabel(number);
    }

    private void setContactID(int id){contactID = id;}
    public int getContactID(){ return contactID;}

    private void setNameLabel(String name){nameLabel = name;}
    public String getNameLabel(){ return nameLabel;}

    private void setNumberLabel(String number){ numberLabel = number;}
    public String getNumberLabel(){ return numberLabel;}
}

Problem with JME is that only Vector, HashMap and array are available. I decided to use Vector as it is more flexible that array for which I need to know size and swapping array elements would can cause more trouble then I want.
So when user select View Contacts option, first I fetch contacts from RMS and store them in ContactListLabel objects and then add them to Vector. Once all contacts are read I pass this vector to sorting method as seen below.

public Vector sortContacts(Vector v){
        Vector sorted = new Vector();
        ContactListLabel cll = (ContactListLabel) v.elementAt(0);
        ContactListLabel cll2 = (ContactListLabel) v.elementAt(1);
        if(cll.getNameLabel().compareTo(cll2.getNameLabel()) <= 0){
            sorted.addElement(cll);
            sorted.addElement(cll2);
        }
        else{
            sorted.addElement(cll2);
            sorted.addElement(cll);
        }

        for(int i = 2; i < v.size(); i++){
            for(int y = 0; y < sorted.size()-1; y++){
                cll = (ContactListLabel)sorted.elementAt(y);
                cll2 = (ContactListLabel)sorted.elementAt(y+1);
                ContactListLabel toInsert = (ContactListLabel)v.elementAt(i);

                if(cll.getNameLabel().toLowerCase().compareTo(toInsert.getNameLabel().toLowerCase())> 0){
                    sorted.insertElementAt(toInsert, y);
                    y = sorted.size();
                }
                else if(cll.getNameLabel().toLowerCase().compareTo(toInsert.getNameLabel().toLowerCase()) <= 0 &&
                        cll2.getNameLabel().toLowerCase().compareTo(toInsert.getNameLabel().toLowerCase())>0){
                    sorted.insertElementAt(toInsert, y+1);
                    y = sorted.size();
                }
                else if(cll2.getNameLabel().toLowerCase().compareTo(toInsert.getNameLabel().toLowerCase()) <= 0 && y+2 == sorted.size()){
                    sorted.addElement(toInsert);
                    y = sorted.size();
                }
            }
        }
        return sorted;
    }

As you can see it is simple insertion. Have look at it and if you think there is some worth to change let me know. It doesn't matter if it is algorithm or my coding. I'm looking for all feedbacks!

I don't know if this suggestion applies to your problem, but you can try this:

Have the class implement the Comparable interface:

public class ContactListLabel implements  Comparable<ContactListLabel > {

public int compareTo(ContactListLabel label) {
  return nameLabel.compareTo(label.getNameLabel());
}
}

When you get the Contacts from RMS and store them in ContactListLabel objects, can you put them in an array instead of a Vector?
If you can, you can use this: Arrays.sort(Object[] a) to do the sorting.
Or after you have the Vector with all the Contacts put them in an array and sort them with the above method.

Again, I have never used JME and I don't know if you can use this solution, but if sorting is your only problem then it is not very difficult to simplify your solution

----------------------------------------------------------------------------

Another thing you can do is sort them the moment you get them from RMS:

> get contact from RMS
> search the Vector with the objects using the nameLabel and find at which index it needs to be inserted
> do the insertion at the index found.

That way you don't need to call the sort method.

If the Vector has, for example, elements:
"a", "b", "d"

And you get the "c" you will search the Vector to find where the "c" needs to be inserted:
if ("c"<"d") put it where the "d" is, and the "d" will automatically shifted to the right:

for (int i=0;i<v.size();i++) {
  if ("c".compareTo(v.get(i))<0) {
     v.add(i, "c");
  }
}

If the vector is empty you will simply insert the first element without checking anything. Then as you get more contacts you will add them to the vector. This algorithm works only for vectors that are already sorted, but you don't need to worry about that because you are the one inserting the elements and you insert them at the right place from the begining.
Also, since you have implemented the Comparable interface you can do this:

ContactListLabel labelFromRMS = .....;
..
if (labelFromRMS.compareTo(v.get(i))<0);
..

Good attempt, unfortunately there is no chance of using interface Comparable of Arrays method sort() as these do not exists in tight specification of JME.
I did consider try to sort contacts as I read them from RMS, but I though it would be more burden as I would either have to keep sorting in same method or I would have to be passing and receiving Vector from caller method (the one that is retrieving contacts) and worker method (sorting). I think it would just unnecessary drain system of valuable resources.

Thank you javaAddict, I appreciate your input on this topic

Can't you use the Vector argument by reference ? :

Vector v = new Vector();
method(v);
System.out.println(v.get(0));

void method(Vector v) {
  v.add("1234");
}

Maybe it cannot be done since I don't know how JME really works or how you have written your code

I would have done it this way (as long as you can modify the Vector passed in, conceptually, as technically you can).

// could also be void, make Vecotr so that it can be "chained"
	public Vector sortContacts(Vector v) {
	   for(int i = 0; i < v.size() - 1; i++) {
		   ContactListLabel cll = (ContactListLabel)v.elementAt(i);
		   for(int y = i + 1; y < v.size(); y++){
			   ContactListLabel cll2 = (ContactListLabel)v.elementAt(y);
			   if(cll.getNameLabel().toLowerCase().compareTo(cll2.getNameLabel().toLowerCase())> 0){
				   v.set(i, cll2);
				   v.set(y, cll);
				   cll = cll2;
			   }
		   }
	   }
	   return v;
	}

A slight correction, since set returns the previous element.

// could also be void, make Vecotr so that it can be "chained"
	public Vector sortContacts(Vector v) {
	   for(int i = 0; i < v.size() - 1; i++) {
		   ContactListLabel cll = (ContactListLabel)v.elementAt(i);
		   for(int y = i + 1; y < v.size(); y++){
			   ContactListLabel cll2 = (ContactListLabel)v.elementAt(y);
			   if(cll.getNameLabel().toLowerCase().compareTo(cll2.getNameLabel().toLowerCase())> 0){
				   v.set(i, cll2);
				   cll = v.set(y, cll);
			   }
		   }
	   }
	   return v;
	}

Very minor difference, but you just well use what Vector provides. ;-)

commented: Neat solution, I :* +16

masijade I think I will try to go your way, but I will need to do some changes because there is no set() method in JME. I think I been to preoccupied with trying to sort the vector fast with no complex algorithm that I went on comparing new element against another two to fit it in, instead of doing one on one. Once again I failed KISS rule

I like this part

v.set(i, cll2);
cll = v.set(y, cll);

At first it does look horribly wrong that you discarding original value at that position, but in reality that one is still in first variable "cll" :D
Beautiful code!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.