# Answered Smallest/lowest number from array

Discussion in 'Call of Duty: Black Ops 3 Answered Questions' started by Cxwh, Nov 30, 2016.

1. ### CxwhActive Member

32
16
8
Credits:
256
How do I get the smallest number of an array? In a "clean" way? Maybe use arraySort idk...

2. ### thahitcrew ModeratorStaff Member

225
257
63
Credits:
195
here's something you could do without the use of any 'helper functions'
Code:
```int smallestNum = 99999999;
const int arrayElements = 5;
int idkExampleArray[arrayElements] = { 122, 8, 24, 6, 65 };
for(int i = 0; i < arrayElements; i++)
{
if(idkExampleArray[i] < smallestNum) smallestNum = idkExampleArray[i];
}
//smallest number will be stored in smallestNum when for loop finishes looping..
//e.g. in this case smallestNum = 6
```
this would work fine. If you're doing it in GSC you just need to change a few things and it should work fine.

Last edited: Dec 1, 2016
LowKeyDNS, CabCon, Cxwh and 1 other person like this.

2,448
1,377
113
Credits:
18,496
@thahitcrew described how to get the lowest number very good.

With this function you can sort an array by their value:
Code:
```function func_sort_by_value( &array, b_lowest_first = false )
{
return merge_sort( array, &func_sort_by_value_compare_func, b_lowest_first );
}

function func_sort_by_value_compare_func( val1, val2, b_lowest_first )
{
if ( b_lowest_first )
{
return val1 < val2;
}
else
{
return val1 > val2;
}
}```
Be sure to include array_shared.gsc

4. ### LowKeyDNSGuest

Credits:
0
CabCon's solution will work, however, there is a much faster sort for larger arrays called a quicksort, which uses ONlogN as in comparison to the merge sort, ON^2. You can find this function in the array_shared.gsc as well, and code for implementation is as follows:

Code:
```#using scripts/shared/array_shared;

function SortWrapper( array )
{
return array::quickSort( array ); //You may optionally define your own comparison function
}

function GetLowestBySort( array )
{
return SortWrapper( array )[ 0 ]; //Get the first element in the array, which is our lowest
}
```
Hope this helps!

SCP and CabCon like this.
5. ### thahitcrew ModeratorStaff Member

225
257
63
Credits:
195
Mine does too lmao. You're saying that as if my solution will not work. Like i mentioned my method of getting lowest number doesn't make use of any helper functions and/or may not be the fastest, but it works 100% completly fine

LowKeyDNS likes this.

2,448
1,377
113
Credits:
18,496
That's a very elegant solution! Top answer. I don't know this way, thank you! Good job!

Also @thahitcrew way, is working also for sure!

7. ### LowKeyDNSGuest

Credits:
0
I didn't mean it that way at all . There are always several ways to solve a problem, and I just wanted to help out!

CabCon and thahitcrew like this.