Find highest number between several strings

Post your questions and help other users.

Moderator: Martin

Post Reply
Lucy
Posts: 225
Joined: 10 Aug 2019 11:24

Find highest number between several strings

Post by Lucy » 27 Nov 2019 05:03

How do i go about finding the highest number between several different strings? I understand basic (if more than, if less than, equals, not, etc) but have absolutely no idea how to find the highest number

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: Find highest number between several strings

Post by Desmanto » 27 Nov 2019 18:37

Highest number from strings? Maybe you mean from list of numbers?

If it is from list of number, there are several way to do it. The quickest one is to sort the list and then take the last element. But I have to specify the lenght-1 in the index, and I prefer to keep the index cleaner. So i prefer to reverse it again and take index 0 instead.

Code: Select all

list = newList(3, 42, 17,15, 35);
max = reverse(sort(list))[0];
This is method is destructive, as it changes the order of the number in the list. If you need to retain the original list order, you have to copyList() first.

Code: Select all

list = newList(3, 42, 17,15, 35);
max = reverse(sort(copyList(list)))[0];
This way, the list order is maintainted.

If you need to get the index of the max number, you have to loop upon the list and compare each of them. It is a bit longer and maybe not what you need here.
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

Lucy
Posts: 225
Joined: 10 Aug 2019 11:24

Re: Find highest number between several strings

Post by Lucy » 27 Nov 2019 18:58

Sorry i worded that terribly.
I have a log file that consists of a fair bit of data, mostly alphabetical. However parts of the log contain numeric data. I am trying to find the highest of the numeric data stored in that file without reading it.
Hope this is more understanding

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: Find highest number between several strings

Post by Desmanto » 28 Nov 2019 17:41

The number all are normal numbers without comma/dot ? If yes, then it can be done easily with simple regex.

Code: Select all

s = "The answer is 42. 3 pineapple. 87 group";
f = findAll(s, "\\d+");
max = reverse(sort(f));
replace the s with the string you want to find the highest number.
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

Lucy
Posts: 225
Joined: 10 Aug 2019 11:24

Re: Find highest number between several strings

Post by Lucy » 29 Nov 2019 00:12

Hmm i think they are all straight forward numbers... i will have a go at this. Thank you

Post Reply