• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

sorting an array from csv file.

 
Dustin Schreader
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a csv file that i'm reading in and I have got all the items in the file to get stored in a variable and the commas have been taken out with parsing. Each line in the csv file is a new person with personal attributes such as favorite pet. I would Like to sort this array of people by which pet is their favorite. I want all the people who like cats to be sorted out from the ones who like other animals and then alphabetized. I was thinking I could iterate through each line of persons and find their favorite pets in [X] part of the array but how do I take the complete line of the persons name who likes cats and attributes and store it in a new sorted array? Do I do something like this:

 
Dustin Schreader
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do I get the value of the third item in the array into a variable to test it against cat?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you using == on Strings?
 
Dustin Schreader
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah I figured that wouldn't work so well So I revised it a little. This is what I have now.

 
Dustin Schreader
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I've just about got it complete except when the if statement is executed it adds a data row to other arrayList every time the item isn't CS. So I figured the best way to add a dataRow to the other arrayLists is only when CS isn't showing up in the 3rd column. So instead of doing something like this:



So I tried to do this



But that didn't work

 
Rob Spoor
Sheriff
Pie
Posts: 20546
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use == true or == false, use direct comparison:
 
Dustin Schreader
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it working
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic