Jump to content
Mr. Green Gaming

Problem with programming.


MiF

Recommended Posts

I have been stuck with problem about hour already, and without working solution, so i think it's time to ask for help.

Problem is this. I have 2 dimension array ( [inf.] [3] ) where first data is name (first and last name), second is age and third is city. Now i need to sort this array of data in order of age (from older to younger).

I am coding it in java, and i can use all libraries which come with java, but no outside ones.

There might be easy solution for this, but i am just locked up (as we say here in finland) :/

Edited by MiF
Link to comment

What exactly is the problem? Are you searching for a sorting algorithm to use or can't you write decent java code in the first place.

I have alghorithm (which works). Problem itself is this;

i have this 2d array with this data (left to right is first dimension, up to down is second):

post-1211-131041687266.png

I need to sort it to this:

post-1211-131041687659.png

Turning them into an ArrayList data structure is going to make your life a lot easier to begin with.

(Ninja'd)

Okey got to check that out ->

Link to comment

1. make a class to contain your values

2. let that class implement the Comparable interface

3. implement the compareTo() method with your custom sorting

Now, by implementing Comparable, you can use some extra collection features such as this sort method for lists (that includes ArrayList) or )"]this one for simple arrays

There are more ways to do it, like comparators, but they are functionally similar (and not any easier anyway). You should avoid writing your own sorting methods, as the java collection framework is already optimized.

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...