Reputation: 4771
In Java , I want to compare and sort strings that contain a character and a number. For example:
A15, D35, A17, C45, B27, C30 should be sorted
A15 A17 B27 C30 C45 D35. I am not really sure how to compare two of those elements because they contain a string and a number. Can anyone help me please?
Upvotes: 5
Views: 12763
Reputation: 11
public class StringComparator implements Comparator<String>{
String string_char[]= {"a","s","d","f","g","h","j","k","l","z","x","c","v","b","n","m","q","w","e","r","t","y","u","i","o","p"};
public enum DataType{
STRING,INTEGER,SPECIAL_CHAR;
}
public Integer getDataTypePriority(DataType type){
try{
switch (type) {
case SPECIAL_CHAR:
return 1;
case INTEGER:
return 2;
case STRING:
return 3;
default:
break;
}
}catch(Exception e){
e.printStackTrace();
}
return null;
}
public boolean isStringType(String val){
for(String temp: string_char)
if(temp.equalsIgnoreCase(val))
return true;
return false;
}
public DataType getDataType(Character val){
DataType type = null;
try{
Integer.parseInt(val.toString());
type = DataType.INTEGER;
}catch(Exception e){
if(isStringType(val.toString()))
type = DataType.STRING;
else
type = DataType.SPECIAL_CHAR;
}
return type;
}
@Override
public int compare(String new_val, String old_val) {
int result = 0;
try{
if(new_val == null)
return -1;
else if(old_val == null)
return 1;
StringBuilder old_Int = new StringBuilder();
StringBuilder new_Int = new StringBuilder();
DataType oldDataType = null;
DataType newDataType = null;
int old_length = old_val.length();
int new_length = new_val.length();
int max = old_length;
if(new_length < old_length)//equals
max = new_val.length();
StringBuilder old_Char = new StringBuilder();
StringBuilder new_Char = new StringBuilder();
for(int i=0;i<max;i++){
old_Char.setLength(0);
new_Char.setLength(0);
old_Char.append(old_val.charAt(i));
new_Char.append(new_val.charAt(i));
oldDataType = getDataType(old_val.charAt(i));
newDataType = getDataType(new_val.charAt(i));
if(oldDataType != newDataType){
if(getDataTypePriority(newDataType) > getDataTypePriority(oldDataType))
return 1;
else
return -1;
}else{//same
if(newDataType.equals(DataType.STRING)){
if(old_Int.length() >0){//clearing string builder
old_Int.setLength(0);
new_Int.setLength(0);
}
if(new_Char.toString().compareTo(old_Char.toString()) >0)
return 1;
else if(new_Char.toString().compareTo(old_Char.toString()) < 0)
return -1;
}else if(newDataType.equals(DataType.INTEGER)){
old_Int.append(old_val.charAt(i));
new_Int.append(new_val.charAt(i));
while(i+1<max){
i+=1;
oldDataType = getDataType(old_val.charAt(i));
newDataType = getDataType(new_val.charAt(i));
if(oldDataType.equals(DataType.INTEGER))
old_Int.append(old_val.charAt(i));
if(newDataType.equals(DataType.INTEGER))
new_Int.append(new_val.charAt(i));
if(oldDataType != newDataType)
break;
}
if(new_length > max){
while(i+1 < new_length){
i+=1;
newDataType = getDataType(new_val.charAt(i));
if(newDataType.equals(DataType.INTEGER))
new_Int.append(new_val.charAt(i));
else
break;
}
}else if(old_length >max){
while(i+1<old_length){
i+=1;
oldDataType = getDataType(old_val.charAt(i));
if(oldDataType.equals(DataType.INTEGER))
old_Int.append(old_val.charAt(i));
else
break;
}
}
Integer n = Integer.parseInt(new_Int.toString());
Integer o = Integer.parseInt(old_Int.toString());
if(n > o)
return 1;
else if(n < o)
return -1;
}else{//special char
if(old_Int.length() >0){//clearing string builder
old_Int.setLength(0);
new_Int.setLength(0);
}
if(new_Char.toString().compareTo(old_Char.toString()) >0)
return 1;
else if(new_Char.toString().compareTo(old_Char.toString()) < 0)
return -1;
}
}
}
return new_val.compareTo(old_val);
}catch(Exception e){
e.printStackTrace();
}
return result;
}
}
Upvotes: 0
Reputation: 1098
Basically, if you need to sort list of string you can use streams:
List<String> yourSortedListOfStrings = yourListOfStrings.stream()
.sorted()
.collect(Collectors.toList());
Upvotes: 0
Reputation: 2515
An example of said compare function:
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import com.google.common.collect.Lists;
class Scratch {
public static void main(String[] args) {
final List<String> in = Lists.newArrayList("D35", "A1", "C7", "A25", "A131");
Collections.sort(in, new Comparator<String>() {
@Override
public int compare(String left, String right) {
int letter = Character.compare(left.charAt(0), right.charAt(0));
if (0 != letter)
return letter;
return Long.compare(Long.parseLong(left.substring(1)), Long.parseLong(right.substring(1)));
}
});
System.out.println(in);
}
}
Upvotes: 4
Reputation: 993085
If your numbers are always two digits, then simply compare the whole things as strings. Numbers in decimal are strings too.
It's a different story if you need to sort A9, A54, and A123456 and want the numbers to sort by their numeric value. In that case you may need to write your own compare function that splits the string apart into its component parts.
Upvotes: 6