Help with sorting an array
in
Programming Questions
•
1 year ago
Hi. I've been having some trouble with this and hoping for some help. Here's my situation:
I have a .txt file with data organized in lots of columns (30+) and lots of rows (150+). I am using the following commands to parse the data:
lines = loadStrings("datafile.txt");
then later:
for (int i=2; i<lines.length; i++) { // I start with 2 rather than 0 because there are 2 rows of header info in the file
String[] pieces = split(lines[i], '\t'); // parsing each piece of data across the row
float X = float(pieces[5]); // defining a variable called X for column #5
float Y = float(pieces[8]); // defining a variable called Y for column #8
This works fine but I get (and display) the data in the order that it happens to be in the file. I want to sort the data in column 5 in descending order. And then later sort the data in column 8 in descending order.
How do I do this? I've looked up past posts on the sort() function but can't seem to get it to work.
I decided to not post my code because the code is very long and complicated and I am trying to simplify the challenge above.
Thanks very much for any help!
Kevin
I have a .txt file with data organized in lots of columns (30+) and lots of rows (150+). I am using the following commands to parse the data:
lines = loadStrings("datafile.txt");
then later:
for (int i=2; i<lines.length; i++) { // I start with 2 rather than 0 because there are 2 rows of header info in the file
String[] pieces = split(lines[i], '\t'); // parsing each piece of data across the row
float X = float(pieces[5]); // defining a variable called X for column #5
float Y = float(pieces[8]); // defining a variable called Y for column #8
This works fine but I get (and display) the data in the order that it happens to be in the file. I want to sort the data in column 5 in descending order. And then later sort the data in column 8 in descending order.
How do I do this? I've looked up past posts on the sort() function but can't seem to get it to work.
I decided to not post my code because the code is very long and complicated and I am trying to simplify the challenge above.
Thanks very much for any help!
Kevin
1