• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Sorting

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following code where the integer arrays are internally sorted in ascending order. I also want the list to be sorted in ascending order. Currently the list looks like [[-8, 3, 5], [-6, 1, 5], [-8, 2, 6]]. Although the arrays are internally sorted, I want the list also to be sorted as [[-8, 2, 6], [-8, 3, 5], [-6, 1, 5]]. Is it possible? I dont want to use a custom class. I want to return the modified List<Integer[]>.

 
Marshal
Posts: 69411
276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Of course you can sort a List like that. I suggest you go through this page from the Java™ Tutorials first.

Suhaas Parekh wrote:. . . I dont want to use a custom class. . . .

You are going to have to create something customised, whether it is a class implementing Comparator<int[]> or a λ to create such an object. That won't be too difficult, but you need to consider a few more questions:-
  • 1: What will you do if any of the elements in the List are null.
  • 2: What you will do if any of the arrays has length 0.
  • 3: What you will do if two arrays are different lengths and the shorter is a prefix of the longer array.
  • 4: Whether you can insist all the arrays be the same length.
  •  
    Campbell Ritchie
    Marshal
    Posts: 69411
    276
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Actually, since you are adding the same array thrice, you won't have any difficulty sorting the List, as you will find out when you print it out
    Use array initialisers rather than assigning the individual elements. Much faster to write and less errorߛprone.
     
    Saloon Keeper
    Posts: 7175
    65
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    There is a method: Arrays.compare( T[] a1, T[] a2 ) which will compare two arrays. You can use that to make your Comparator.

    You are adding the same array three times to your list. That's going to cause a problem.

    Here's a way to initialize your list:
     
    Suhaas Parekh
    Greenhorn
    Posts: 8
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Someone please help me with the comparator. I have the list of array as [[-8, 3, 5], [-6, 1, 5], [-8, 2, 6]] which is internally sorted in ascending order. I want to write a comparator to have the list also sorted in ascending order like [[-8, 2, 6], [-8, 3, 5], [-6, 1, 5]]

     
    Saloon Keeper
    Posts: 12008
    257
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Why does your comparator compare lists? It's called ArrayComparator, right? That means it should compare arrays, not lists.
     
    Suhaas Parekh
    Greenhorn
    Posts: 8
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I was able to modify the code successfully but I I cannot find Arrays compare method in java doc(Javadoc

     
    Stephan van Hulst
    Saloon Keeper
    Posts: 12008
    257
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You're browsing the standard API documentation for Java 8. Arrays.compare() was added in Java 9.
     
    Carey Brown
    Saloon Keeper
    Posts: 7175
    65
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

     
    Bartender
    Posts: 3959
    155
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Suhaas Parekh wrote:I was able to modify the code successfully but I I cannot find Arrays compare method in java doc(Javadoc


    This is the old (< java8) way of doing (nothing wrong with it). Nowadays you would use:
     
    Suhaas Parekh
    Greenhorn
    Posts: 8
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    If suppose I was using Java 8 how can I do the comparison?
     
    Piet Souris
    Bartender
    Posts: 3959
    155
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Well, the old fashioned way: comparing element by element, and as soon as two elements differ, return -1 or 1. And if the arrays are equakl up to the end of the shortest array, then decide what to return.
     
    Suhaas Parekh
    Greenhorn
    Posts: 8
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I have written tbe below code. Is there a better way?

     
    Carey Brown
    Saloon Keeper
    Posts: 7175
    65
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I would suggest doing with a loop so that the method is not specific to an array of length 3.
     
    Campbell Ritchie
    Marshal
    Posts: 69411
    276
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I can hardly imagine a worse way, I am afraid. Subtraction is unreliable because you can suffer an overflow error. Integers already possess a compareTo() method. Use that instead. Don't guess about the lengths of the arrays: use a loop. ...and that isn't correct code because it doesn't take all sorts of corner cases into consideration
     
    a wee bit from the empire
    Devious Experiments for a Truly Passive Greenhouse!
    https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
      Bookmark Topic Watch Topic
    • New Topic