Biggest Number From A Selection

Nyptop

Active Member
Licensed User
Longtime User
Hi there,

I was wondering if anyone could give an example of how you can program an app to identify the largest value from a group of integers (int).

At the moment I am having to write things like:

B4X:
If (a < b) AND (a < c) Then

This process would be fine for a couple of numbers but I have many integers and I think I'll be writing forever if I have to do this for every option.

If anyone knows a way to write 'less than or equal to' or 'more than or equal to' out of a group of int's that would also be greatly appreciated :)

Cheers,

Neil
 

Nyptop

Active Member
Licensed User
Longtime User
From a bit more searching I think the way to sort the data would be through quick sort (or some similar algorithm). My dilemma now is how to tell what the smallest or largest number is out of the sorted numbers. Any ideas?
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
Instead of having seperate variables for each int, why not put them in a map. You can then iterate through the map to find the largest or smallest value, and retrieve its name. Have a read up on them in the docs.
 
Upvote 0

Bill Norris

Active Member
Licensed User
Longtime User
RE:

Following gives you an idea of how to do. Since I don't know how you are getting your values, I can't tell you how to read them into the list, but I think you'll get the general idea.



B4X:
Dim mylist As List
Dim largest_number as Double 'or Int if appropriate

mylist.Add() 'puts an item into the list. You can use AddAll if  your numbers are already in an array -- refer to the tool tips as you code

'after all items are in the list, then
mylist.sort(false) 'false sorts list Descending, true sorts list Ascending, 
largest_number=mylist.get(0) 'list index is zero based. Largest number will be first item in the list since we sorted Descending
 
Upvote 0

Nyptop

Active Member
Licensed User
Longtime User
Thanks for the help!

I'm going with the list because I haven't worked out the mapping yet :)

Cheers,

Neil
 
Upvote 0
Top