# Listbox - Extracting Max & Min Values

I have a windows app that imports a csv file into a listbox. Column 0 contains dates/times and column 1 contains data. It may look like this:

01/26/2015 3:00 am, 1.97
01/26/2015 3:15 am, 3.97
01/26/2015 3:30 am, 0.88
… and so on.

I currently convert the string to a double and can obtain the total and average of the column. I would like an efficient way to get the max and min value from the same column (column 1).

Thanks !

Does sorting on column 1 give you correctly sorted data? If so then you can select row 0 cell 1 value and last row cell 1 depending on which way it was sorted for max and min. Not with a machine currently to check if number or string based for cell values which would affect sorting.

Thanks Paul, but i am not going to sort the data or change the order if thats what you mean. The date /time is the left column (0) and the data is column 1. I will not ever change the presentation of the list as the date and time are sequentially listed in 15 minute increments. I want to read the data column and find the max and min. from there i will have the results shown in their respective labels for display.

Depending on the number of rows you could copy the column 1 numbers to a double array, sort it and find max and min and just rewrite the array each time you need to check which may or may not be time consuming depending on number of rows.

It is data from a logger and could be as few as 100 or as many as thousands and thousands of records. A snippet would be great if someone has done this. Thanks again.

Since you’re looping through the rows to get total and average anyway, keep track of min/max along the way.

``````dim mn, mx as double
dim row as integer

for row = 0 to listbox1.listcount
// extract d as double
mx = max(mx, d)
mn = min(mx, d)
if row = 0 then mn = d    // special case for first min value
next``````

Why not add your logs into SQLiteDatabase. then you have the opportunity to get Min/ Max value very fast plus other options and use the listbox as a window to your SQLLiteDatabase .

Thanks John. I deal with customers who basically will be going on site with Windows tablets to transfer logged data from an embedded system to the tablet for report processing. The reason I use a .csv file for the stored data is simplicity as well as portability. I try to keep a flat file format so the user can simply load a file and run reports, etc. without the overhead of a database. My data files are sequential (time based), and not really transactional, so I have always liked the idea of opening a discrete file, process it, run reports, graphs, etc., and if need be, share the file via email or whatever without the worry of a database. It has been many years since I have ran a software development project (I am getting back into it), so maybe SQLite is more portable than what I was used to years ago.

Tim gave me a great solution that I am wrestling with right now. But I am certainly open to looking into SQLite as well.

Thanks again,

John

@John Marsh

That’s the beauty of SQLiteDatabase. SQLite database is just a single file (no daemon is running), which you can copy to any kind of platforms and read it with your SQLite client that is installed on that platform. More info see: https://www.sqlite.org/

Subclass the listbox.
Add a MinVal and MaxVal property

In the class, watch for text changing in the relevant column.
When the text changes,

```dim thisval as double thisval = val(cell(row,col) ) if thisval > MaxVal then MaxVal =thisval if thisval < MinVal then MinVal = thisval```

So it basically updates itself while you add rows, and if you allow the cell to be editted too