• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Sorting ArrayLists

 
Ranch Hand
Posts: 2274
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following sort class works fine when I use regular arrays however:

I get the following message when using ArrayList on the line that contains 'Comparable[] array1 = (Comparable[])o1;':

java.lang.ClassCastException: java.util.ArrayList incompatible with [Ljava.lang.Comparable;

I build my ArrayList of ArrayList then I call the following sort class:



My sort class:

 
Sheriff
Posts: 28385
99
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, yeah. The comparator is written to assume it's going to be given arrays to compare, or what you call "regular arrays". But an ArrayList isn't an array of any kind, despite its name, hence the error message.
 
Steve Dyke
Ranch Hand
Posts: 2274
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:Well, yeah. The comparator is written to assume it's going to be given arrays to compare, or what you call "regular arrays". But an ArrayList isn't an array of any kind, despite its name, hence the error message.



Can you guide me in a sort soultion for ArrayList
 
Paul Clapham
Sheriff
Posts: 28385
99
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Replace



by



and make the corresponding changes in the following lines.
 
Steve Dyke
Ranch Hand
Posts: 2274
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:Replace



by



and make the corresponding changes in the following lines.



I am really struggling. What if I just want to sort my ArrayList<ArrayList<String>> on the first column assending?
 
Paul Clapham
Sheriff
Posts: 28385
99
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sorry, I don't understand that question. The first column of what?
 
Marshal
Posts: 80508
455
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is something wrong when you are trying to sort columns in an object‑oriented language. Those columns should be incorporated into a class, and objects created from that class, which you can apply Comparators to. Then you can sort an ordinary array.
 
Steve Dyke
Ranch Hand
Posts: 2274
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:I'm sorry, I don't understand that question. The first column of what?



I picture the ArrayList<ArrayList<String>> as having elements separated by commas with each iteration of the first ArrayList as being the row. So I would like to re-arange the rows based on the first string of each element.

The strings should be as:

862356-401, Seat Assy
854698-41, Bracket
859365-1, Angle

So I want to sort these strings in asending order by the part number.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steve Dyke wrote:I picture the ArrayList<ArrayList<String>> as having elements separated by commas with each iteration of the first ArrayList as being the row. So I would like to re-arange the rows based on the first string of each element.

The strings should be as:
862356-401, Seat Assy
854698-41, Bracket
859365-1, Angle

So I want to sort these strings in asending order by the part number.


OK, there's a few things going on here:
1. Collections.sort() has no way of knowing that your first String is, in fact, a Part Number, so it will sort it as a String, which means that
862356-401 will sort before
862356-41
If you're OK with that, I don't see any particular problem with creating a Comparator to compare two Lists based on a particular element, viz:then your code might look something like:However, there are several possible 'gotchas', particularly if rows don't always have the same number of columns, and the whole thing feels a bit 'clunky' to me.

HIH

Winston
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steve Dyke wrote:

Paul Clapham wrote:I'm sorry, I don't understand that question. The first column of what?



I picture the ArrayList<ArrayList<String>>



Except as already stated you shouldn't have a list of lists. Instead you should have something like List<Product>, where the Product class defines productId and productName member variables, or whatever those columns represent.
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steve Dyke wrote:

Paul Clapham wrote:I'm sorry, I don't understand that question. The first column of what?



I picture the ArrayList<ArrayList<String>> as having elements separated by commas with each iteration of the first ArrayList as being the row. So I would like to re-arange the rows based on the first string of each element.

The strings should be as:

862356-401, Seat Assy
854698-41, Bracket
859365-1, Angle

So I want to sort these strings in asending order by the part number.




While this discussion into the design, and the requirements, is a really a good idea -- I am wondering if this is a detour of the original question.

In the original question, you have a working comparator that compares using Comparable arrays, and the elements in the array are the "columns". So, to convert it to using an arraylist, isn't it just a 5 minute refactoring exercise? Just use a List<Comparable> and have the elements (based on index) of the list to be the "columns"?

Henry
 
Steve Dyke
Ranch Hand
Posts: 2274
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for all the help.

I have it working by using the following:

reply
    Bookmark Topic Watch Topic
  • New Topic