• Welcome to DictionaryForMids Forum.
 

Java ME: sorting of Vector elements and filtering of duplicates

Started by Gert, 20. February 2007, 07:13:48

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Gert

I need to have an implementation for an algorithm that does sorting and filtering of duplicate entries of a Vector.
I have an (unsorted) Vector with objects of 'SomeClass'. The elements of the Vector need to get sorted. Also, if in the Vector two (or more) identical objects exist, then only one shall remain there, the other occurences shall be removed from the Vector.

Of course the algorithm should efficient. For sorting the quick sort algorithm should be fine.

I can provide a method such as compareTo in SomeClass, or whatever method is needed for the comparision of the entries.

I do need an implementation for Java ME (CLDC/MIDP), please don't point me to existing features of the Java SE Collection framework.

Anyone who can provide me with an implementation ?

Thanks !
Gert


Stefan1200

I don't found an online documentation of the Java ME API. But if the Vector has the indexOf() method, my Java SE solution should work. Don't know if this is the best solution, but works for me:

It does not sort, but this can be done after or before calling this method.
tmpDB Vector is needed to delete double entries case insensitive. If this should be case sensitive, tmpDB Vector is not needed!
private void deleteDoubleEntries(Vector dictHistory)
{
Vector tmpDB = new Vector();

for (int i=0; i<dictHistory.size(); i++)
{
tmpDB.addElement(dictHistory.elementAt(i).toString().toUpperCase());
}

for (int i=0; i<dictHistory.size(); i++)
{
int tmp = tmpDB.indexOf(tmpDB.elementAt(i),i+1);
if (tmp != -1)
{
tmpDB.removeElementAt(tmp);
dictHistory.removeElementAt(tmp);
i--;
}
}
}