Sort an Array of Strings by Their Integer Values

Sort a String array, whose strings represent int

I think by far the easiest and most efficient way it to convert the Strings to ints:

int[] myIntArray = new int[myarray.length];

for (int i = 0; i < myarray.length; i++) {
myIntArray[i] = Integer.parseInt(myarray[i]);
}

And then sort the integer array. If you really need to, you can always convert back afterwards:

for (int i = 0; i < myIntArray.length; i++) {
myarray[i] = "" + myIntArray[i];
}

An alternative method would be to use the Comparator interface to dictate exactly how elements are compared, but that would probably amount to converting each String value to an int anyway - making the above approach much more efficient.

Sort an Array of Strings by their Integer Values

I'll throw another method out there since it's the shortest way I can think of

a.sort_by(&:to_i)

Sort array with string keys and integer values

You should not use strings as "keys"/"indices" in an array as they will not be accessed by the default array methods. Arrays use numeric indices to reference a value. You could use a object as key value storage. In objects you can use strings as keys to reference a value.
You could then sort the object as follows.

let numbers = {};
numbers["a"] = 5;
numbers["ld"] = 2;
numbers["la"] = 3;

numbers = Object.fromEntries(Object.entries(numbers).sort((a, b) => {
return a[1] - b[1]
}));

Sorting array of strings that contain number

You can do something like that:

list.sort(Comparator.comparing(YourClass::removeNumbers).thenComparing(YourClass::keepNumbers));

These are two methods:

private static String removeNumbers(String s) {
return s.replaceAll("\\d", "");
}

private static Integer keepNumbers(String s) {
String number = s.replaceAll("\\D", "");
if (!number.isEmpty()) {
return Integer.parseInt(number);
}
return 0;
}

For following data:

List<String> list = new ArrayList<>();
list.add("TEXT-2");
list.add("TEST-6");
list.add("TEST-1");
list.add("109");
list.add("TE");
list.add("TESTT-0");
list.add("TES-100");

This is the sorting result:

[109, TE, TES-100, TEST-1, TEST-6, TESTT-0, TEXT-2]

Sorting a String list with Integer values in Java

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); **<------**

How to sort an array of integers correctly

By default, the sort method sorts elements alphabetically. To sort numerically just add a new method which handles numeric sorts (sortNumber, shown below) -

var numArray = [140000, 104, 99];
numArray.sort(function(a, b) {
return a - b;
});

console.log(numArray);

Sort array of strings by integer (which is the first character)

Use regex to get the numbers and after it compare the numbers.

const array = [ "15 Some string", "16 Some string", "13 Some string", "11 Some string", "6 Some string", "8 Some string", "12 Some string", "5 Some string", "9 Some string", "10 Some string" ];
array.sort((a,b) => b.match(/\d+/g) - a.match(/\d+/g));
console.log(array);

How to sort an array of Strings based on the parsed first digit

You can split each string on \D (which means non-digit) and compare the strings based on the first elements, parsed into an integer, of the resulting arrays.

Demo:

import java.util.Arrays;

public class Main {
public static void main(String[] args) throws InterruptedException {
String[] array = { "18.sdfahsdfkjadf", "1.skjfhadksfhad", "2.asldfalsdf" };

Arrays.sort(array,
(s1, s2) ->
Integer.compare(
Integer.parseInt(s1.split("\\D")[0]),
Integer.parseInt(s2.split("\\D")[0])
)
);

System.out.println(Arrays.toString(array));
}
}

Output:

[1.skjfhadksfhad, 2.asldfalsdf, 18.sdfahsdfkjadf]


Related Topics



Leave a reply



Submit