I have a column VALUE in my table that contains:
`M_SYSCONFIG = 200600,2600000,700000600,110000600,150000600`
When I sort this list the result is: 110000600,150000600,110000600,200600,2600000,700000600
However, I need the list to be sorted as follows (treat the strings as integers): 200600,2600000,110000600,150000600,700000600
This is the code I have right now for sorting the list:
JSONArray jsArray = dbcon.callSelectRecords("SELECT CODE, VALUE FROM M_SYSCONFIG WHERE MODULE = 'LIMIT_CONFIG' AND CODE in (?,?,?,?) ORDER BY VALUE", ft_other_cn, ft_own_account, payment, purchase);
for (int i = 0; i< jsArray.size(); i++) {
JSONObject js = JSON.newJSONObject(jsArray.get(i).toString());
String trasactionType = JSON.get(js, "CODE");
String value = JSON.get(js, "VALUE");
List<String> data = Arrays.asList(value.split(","));
Collections.sort(data);
I need to obtain the results as strings because after sorting I want to apply the following code:
StringBuilder sbValue = new StringBuilder();
if(ft_other_cn.equals(trasactionType)) {
long limitOtherCimb = limit.getFtOtherCimbLimit();
sbValue.append(limitOtherCimb).append(",");
for(String values:data) {
Long limitSysConfig = null;
try {
limitSysConfig = Long.parseLong(values);
} catch (Exception e) {}
if(limitSysConfig == null) {
continue;
}
if(limitSysConfig > limitOtherCimb) {
continue;
}
sbValue.append(limitSysConfig).append(",");
}
customerLimit.setFtOtherCnLimit(StringUtils.removeEnd(sbValue.toString(), ","));
I suggest using biginteger because your numbers seems quite large. It's not the most efficient and optimized solution but yeah it will work
public static List<String> sortData(List<String> data){
List<BigInteger>convertedData=new ArrayList<BigInteger>();
for (String s : data)
{
//System.out.println(s);
convertedData.add(new BigInteger(s));
}
Collections.sort(convertedData);
List<String>sortedData=new ArrayList<String>();
for (BigInteger b : convertedData)
{
sortedData.add(String.valueOf(b));
}
return sortedData;
}
Your code:
JSONArray jsArray = dbcon.callSelectRecords("SELECT CODE, VALUE FROM M_SYSCONFIG WHERE MODULE = 'LIMIT_CONFIG' AND CODE in (?,?,?,?) ORDER BY VALUE", ft_other_cn, ft_own_account, payment, purchase);
for (int i = 0; i< jsArray.size(); i++) {
JSONObject js = JSON.newJSONObject(jsArray.get(i).toString());
String trasactionType = JSON.get(js, "CODE");
String value = JSON.get(js, "VALUE");
List<String> data = Arrays.asList(value.split(","));
List<String> sortedData=sortData(data); **<------**
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments