• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Sort on java

 
Greenhorn
Posts: 4
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, im new to java and wanted to ask a question about my code. I need to take a 4 column from massive and sort it. I have massive, but im not sure about my next steps, am i doing it right, and how should i do a sort?

My massive code


And here im trying to get numbers from 4 column

 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please explain what massive is. What are these four columns? What size are they?
Why are you doing all that arithmetic with Math#random()? I would prefer to use
 
Mantas Uzmirkis
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
im doing it only because it is task from university.


This is my massive, and i need to print and sort fourth column
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So how are those numbers represented? As an int[][]? Do you know how to get the 4th element of any array?
 
Mantas Uzmirkis
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is int[][] mas = new int[n][m] where n is rows and m is columns

to get fourth column i was using this code


But im not sure if it is good one
 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
col = 4; // This is the 5th column because indexes start at zero
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might want to look at the Javadoc for Arrays#sort().

You'd need to create a class that implements java.util.Comparator and have that compare the 4th column.
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Write a method that gives you a column from a 2D array, like:

(you already showed the code to do that)

Then you can do for column 4:

Sorting an int[] with Arrays.sort does not require a Comparator, so no worries here   Arrays.sort(int[])
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you allowed to use Arrays#sort()? Do you have to write your own sorting algorithm? For a small array, the simplest algorithms will work better than the more sophisticated ones.
 
Mantas Uzmirkis
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Are you allowed to use Arrays#sort()? Do you have to write your own sorting algorithm? For a small array, the simplest algorithms will work better than the more sophisticated ones.



Yes, i can use Arrays#sort and bubble sort
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using a Comparator you have to specify the type that you are going to sort. You have an array of arrays of ints, so for each row you have an int[], and that's what the Comparator will get as the type. You could hard code the column index to sort on but this example uses the constructor to set the column index.

 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd try to decompose problem to smaller problems.

For instance:
1. Get column given its position within the table i.e. int[] getColumn(int[][] table, int position)
2. Sort column (really just an array). There are already methods for that mentioned in this thread.
3. Replace unsorted column with sorted within the table i.e. replaceColumn(int[][] table, int atPosition, int[] column)

All those tasks on their own aren't that complicated as a one big task.
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interesting. Carey sorts the rows based on a column. So, the question to OP is. can you give some more explanation?

@Carey
can you turn your class into a method that returns that comparator?
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

...though I'm not sure how to pass the column index in as an argument.
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Appears to work. I didn't think it would.
 
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Appears to work. I didn't think it would.


Though, that method is vulnerable to int overflow.  A difference like 2_000_000_000 - -2_000_000_000 evaluates as -294967296.  Better to use:
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I was aware of that but it wasn't an issue with this problem domain. However, yours is a more robust way to go.
 
Piet Souris
Bartender
Posts: 5465
212
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
equivalently:
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic