Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Alphanumeric sorting issue

  Asked By: Diane    Date: Jun 28    Category: Java    Views: 1805
  

I followed the below link to sort a set of Items. I have kept
that Items in a HashMap rather then List(this is my requirement). It
works perfectly ok but some items get lost("15", "Alpha 2") while doing
sorting. I would
love to have some comments as to why its happening.

http://www.davekoelle.com/alphanum.jsp

Here is my sample code.

===============================
package ConsoleApplication1;

import java.util.*;


/**
* Summary description for Class1.
*
* @author karthikeyan
* @created May 12, 2004
*/
public class Class1 {
/**
*Constructor for the Class1 object
*/
public Class1() { }


/**
* @param args The command line arguments
* @attribute System.STAThread()
*/
public static void main(String[] args) {
//
// TODO: Add code to start application here
//
HashMap ipcModelHashMap = new HashMap();

ipcModelHashMap.put("1", "1000X Radonius Maximus");
ipcModelHashMap.put("2", "10X Radonius");
ipcModelHashMap.put("3", "200X Radonius");
ipcModelHashMap.put("4", "20X Radonius");
ipcModelHashMap.put("5", "20X Radonius Prime");
ipcModelHashMap.put("6", "30X Radonius");
ipcModelHashMap.put("7", "40X Radonius");
ipcModelHashMap.put("8", "Allegia 50 Clasteron");
ipcModelHashMap.put("9", "Allegia 500 Clasteron");
ipcModelHashMap.put("10", "Allegia 51 Clasteron");
ipcModelHashMap.put("11", "Allegia 51B Clasteron");
ipcModelHashMap.put("12", "Allegia 52 Clasteron");
ipcModelHashMap.put("13", "Allegia 60 Clasteron");
ipcModelHashMap.put("14", "Alpha 100");
ipcModelHashMap.put("15", "Alpha 2");
ipcModelHashMap.put("16", "Alpha 200");
ipcModelHashMap.put("17", "Alpha 2A");
ipcModelHashMap.put("18", "Alpha 2A-8000");
ipcModelHashMap.put("19", "Alpha 2A-900");
ipcModelHashMap.put("20", "Callisto Morphamax");
ipcModelHashMap.put("21", "Callisto Morphamax 500");
ipcModelHashMap.put("22", "Callisto Morphamax 5000");
ipcModelHashMap.put("23", "Callisto Morphamax 600");
ipcModelHashMap.put("24", "Callisto Morphamax 700");
ipcModelHashMap.put("25", "Callisto Morphamax 7000");
ipcModelHashMap.put("26", "Callisto Morphamax 7000 SE");
ipcModelHashMap.put("27", "Callisto Morphamax 7000 SE2");
ipcModelHashMap.put("28", "QRS-60 Intrinsia Machine");
ipcModelHashMap.put("29", "QRS-60F Intrinsia Machine");
ipcModelHashMap.put("30", "QRS-62 Intrinsia Machine");
ipcModelHashMap.put("31", "QRS-62F Intrinsia Machine");
ipcModelHashMap.put("32", "Xiph Xlater 10000");
ipcModelHashMap.put("33", "Xiph Xlater 2000");
ipcModelHashMap.put("34", "Xiph Xlater 300");
ipcModelHashMap.put("35", "Xiph Xlater 40");
ipcModelHashMap.put("36", "Xiph Xlater 5");
ipcModelHashMap.put("37", "Xiph Xlater 50");
ipcModelHashMap.put("38", "Xiph Xlater 500");
ipcModelHashMap.put("39", "Xiph Xlater 5000");
ipcModelHashMap.put("40", "Xiph Xlater 58");
ipcModelHashMap.put("41", "z1.html");
ipcModelHashMap.put("42", "z10.html");
ipcModelHashMap.put("43", "z101.html");
ipcModelHashMap.put("44", "z102.html");
ipcModelHashMap.put("45", "z103.html");
ipcModelHashMap.put("46", "z104.html");
ipcModelHashMap.put("47", "z105.html");
ipcModelHashMap.put("48", "z106.html");
ipcModelHashMap.put("49", "z107.html");
ipcModelHashMap.put("50", "z108.html");
ipcModelHashMap.put("51", "z109.html");
ipcModelHashMap.put("52", "z11.html");
ipcModelHashMap.put("53", "z110.html");

Comparator ipcModelComparator = new
AlphanumComparator(ipcModelHashMap);

Map ipcModelSortedMap = new TreeMap(ipcModelComparator);

ipcModelSortedMap.putAll(ipcModelHashMap);

Set ipcModelSet = ipcModelSortedMap.entrySet();

for (Iterator ipcModelIterator = ipcModelSet.iterator();
ipcModelIterator.hasNext(); ) {
Map.Entry ipcModelMapEntry = (Map.Entry)
ipcModelIterator.next();
String ipcModelKey = (String) ipcModelMapEntry.getKey();
String ipcModelValue = (String) ipcModelMapEntry.getValue();

System.out.println("Key : " + ipcModelKey + " Value : " +
ipcModelValue);
}
}
}


/**
* Description of the Class
*
* @author karthikeyan
* @created May 12, 2004
*/
class AlphanumComparator implements Comparator {


private Map secondaryMap;


/**
*Constructor for the AlphanumComparator object
*
* @param maptosort Description of the Parameter
*/
public AlphanumComparator(Map maptosort) {
secondaryMap = new HashMap(maptosort);
}


char[] numbers = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0'};


/**
* Gets the in attribute of the AlphanumComparator object
*
* @param ch Description of the Parameter
* @param chars Description of the Parameter
* @return The in value
*/
private boolean isIn(char ch, char[] chars) {
for (int i = 0; i < chars.length; i++) {
if (ch == chars[i]) {
return true;
}
}
return false;
}


/**
* Description of the Method
*
* @param ch Description of the Parameter
* @param s Description of the Parameter
* @return Description of the Return Value
*/
private boolean inChunk(char ch, String s) {
if (s.length() == 0) {
return true;
}

char s0 = s.charAt(0);
int chunkType = 0;
// 0 = alphabetic, 1 = numeric

if (isIn(s0, numbers)) {
chunkType = 1;
}

if ((chunkType == 0) && (isIn(ch, numbers))) {
return false;
}
if ((chunkType == 1) && (!isIn(ch, numbers))) {
return false;
}

return true;
}


/**
* Description of the Method
*
* @param o1 Description of the Parameter
* @param o2 Description of the Parameter
* @return Description of the Return Value
*/
public int compare(Object o1, Object o2) {

String s1 = (String) secondaryMap.get(o1);
String s2 = (String) secondaryMap.get(o2);

int thisMarker = 0;
int thisNumericChunk = 0;
String thisChunk = new String();
int thatMarker = 0;
int thatNumericChunk = 0;
String thatChunk = new String();

while ((thisMarker < s1.length()) && (thatMarker < s2.length())) {
char thisCh = s1.charAt(thisMarker);
char thatCh = s2.charAt(thatMarker);

thisChunk = "";
thatChunk = "";

while ((thisMarker < s1.length()) && inChunk(thisCh,
thisChunk)) {
thisChunk = thisChunk + thisCh;
thisMarker++;
if (thisMarker < s1.length()) {
thisCh = s1.charAt(thisMarker);
}
}

while ((thatMarker < s2.length()) && inChunk(thatCh,
thatChunk)) {
thatChunk = thatChunk + thatCh;
thatMarker++;
if (thatMarker < s2.length()) {
thatCh = s2.charAt(thatMarker);
}
}

int thisChunkType = isIn(thisChunk.charAt(0), numbers) ? 1 : 0;
int thatChunkType = isIn(thatChunk.charAt(0), numbers) ? 1 : 0;

// If both chunks contain numeric characters, sort them
numerically
int result = 0;

if ((thisChunkType == 1) && (thatChunkType == 1)) {
thisNumericChunk = Integer.parseInt(thisChunk);
thatNumericChunk = Integer.parseInt(thatChunk);
if (thisNumericChunk < thatNumericChunk) {
result = -1;
}
if (thisNumericChunk > thatNumericChunk) {
result = 1;
}
} else {
result = thisChunk.compareTo(thatChunk);
}

if (result != 0) {
return result;
}
}

return 0;
}
}

Share: 

 

No Answers Found. Be the First, To Post Answer.

 
Didn't find what you were looking for? Find more on Alphanumeric sorting issue Or get search suggestion and latest updates.




Tagged: