File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes How to get my arrays alphabetically sorted? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to get my arrays alphabetically sorted?" Watch "How to get my arrays alphabetically sorted?" New topic
Author

How to get my arrays alphabetically sorted?

Sujath Mohammed
Ranch Hand

Joined: Apr 17, 2013
Posts: 31
So i got my arrays in alphabetical order but the years appear in the same order i want the year to appear depending on which each team has won like this

but the year just seems to go in order here's my program
Saif Asif
Ranch Hand

Joined: Aug 11, 2011
Posts: 440

Have a look at the Arrays.sort( ) method.


Muhammad Saif Asif Mirza
OCJA(5/6) OCJP(6) OCJWCD(6)
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
Arrays.sort() only sorts the array you tell it to sort - it doesn't know anything about any parallel arrays. If you want to sort two parallel arrays you will have to write your own sort function, but the better way to do this is to create a class that contains team name and year fields and create an array of instances of this class and then sort that.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
Did you look at the links about how bad parallel arrys are, which I posted yesterday?
Why are you using shorts? The default type for integers is the int.
Sujath Mohammed
Ranch Hand

Joined: Apr 17, 2013
Posts: 31
Campbell Ritchie wrote:Did you look at the links about how bad parallel arrys are, which I posted yesterday?
Why are you using shorts? The default type for integers is the int.


I am using arrays because the assignment says i have to use them or else i can do what you suggested
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Sujath Mohammed wrote:I am using arrays because the assignment says i have to...

In which case you will need some sort of custom sort; but first you'll need to extract distinct team names from your winners list. One way to do that is to convert it to a Set (java.util.Set) first, viz:(I'll leave you to work out the mechanics)

If you sort that, you'll then have an alphabetical list of winning teams which you can then use to run through your parallel arrays.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Sujath Mohammed
Ranch Hand

Joined: Apr 17, 2013
Posts: 31
Winston Gutkowski wrote:
Sujath Mohammed wrote:I am using arrays because the assignment says i have to...

In which case you will need some sort of custom sort; but first you'll need to extract distinct team names from your winners list. One way to do that is to convert it to a Set (java.util.Set) first, viz:(I'll leave to to work out the mechanics)

If you sort that, you'll then have an alphabetical list of winning teams which you can then use to run through your parallel arrays.

Winston


I don't seem to get what you mean might i tell you i'm new to java so if you explain a bit more please
thank you
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Sujath Mohammed wrote:I don't seem to get what you mean might i tell you i'm new to java so if you explain a bit more please

OK.

You say you want to sort your teams alphabetically; but from what I see, you don't want to sort ALL your winners, you want to sort an array that contains each winning team exactly once (that's what 'distinct' means). Sets (and HashSet is a Set) guarantee this as part of their contract.

Alternatively, you can extract distinct team names yourself - it's not too difficult - but its an array of distinct team names that you need to sort, not all your winners. Indeed, sorting a copy of your winners array may be the easiest way to start.

The fact is that there's more than one way to do this, but what you mustn't do is sort the winners array itself; otherwise you'll screw up all that lovely matching between your two arrays.

Question: were you specifically told to use TWO arrays as you've done, or were you simply told that you must use arrays?

Winston
Sujath Mohammed
Ranch Hand

Joined: Apr 17, 2013
Posts: 31
Winston Gutkowski wrote:
Sujath Mohammed wrote:I don't seem to get what you mean might i tell you i'm new to java so if you explain a bit more please

OK.

You say you want to sort your teams alphabetically; but from what I see, you don't want to sort ALL your winners, you want to sort an array that contains each winning team exactly once (that's what 'distinct' means). Sets (and HashSet is a Set) guarantee this as part of their contract.

Alternatively, you can extract distinct team names yourself - it's not too difficult - but the fact is that its an array of distinct team names that you need to sort, not all your winners. Indeed, sorting a copy of your winners array may be the easiest way to start.

The fact is that there's more than one way to do this, but what you mustn't do is sort the winners array itself; otherwise you'll screw up all that lovely matching between your two arrays.

Question: were you specifically told to use TWO arrays as you've done, or were you simply told that you must use arrays?

Winston


First of all i was given two arrays to use and those are the arrays they provided.
Secondly i want the outcome to be like this
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Sujath Mohammed wrote:First of all i was given two arrays to use and those are the arrays they provided.

Fair enough.

Secondly i want the outcome to be like this

OK. Forget about sorting just for the moment. How would you go about that if you were only given one team name (eg, "Balmain")? Write out what you think you'd need to do and show us the code (or simply the steps, written out in English).

Winston
Sujath Mohammed
Ranch Hand

Joined: Apr 17, 2013
Posts: 31
Winston Gutkowski wrote:
Sujath Mohammed wrote:First of all i was given two arrays to use and those are the arrays they provided.

Fair enough.

Secondly i want the outcome to be like this

OK. Forget about sorting just for the moment. How would you go about that if you were only given one team name (eg, "Balmain")? Write out what you think you'd need to do and show us the code (or simply the steps, written out in English).

Winston


As i said the output should display year and team so if i was only given Balmain and the years they won 1908,1909,1910,1945.
Then it should output the team Balmain 4 times for each year so like this
Year Team
1908 Balmain
1909 Balmain
1910 Blamain
1945 Balmain
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Sujath Mohammed wrote:As i said the output should display year and team so if i was only given Balmain and the years they won 1908,1909,1910,1945...

I understand what you want; I'm saying: given your two arrays, how would you do it for ONE team?

You need to ShowSomeEffort (←click). Nobody here is simply going to hand you the answer.

Winston
Sujath Mohammed
Ranch Hand

Joined: Apr 17, 2013
Posts: 31
Winston Gutkowski wrote:
Sujath Mohammed wrote:As i said the output should display year and team so if i was only given Balmain and the years they won 1908,1909,1910,1945...

I understand what you want; I'm saying: given your two arrays, how would you do it for ONE team?

You need to ShowSomeEffort (←click). Nobody here is simply going to hand you the answer.

Winston


So i had one team i would write a code like this:
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Sujath Mohammed wrote:So i had one team i would write a code like this:...

That's not what I asked. Obviously, you can simply write a custom program for an individual team.

One more time: given your existing arrays, how would you extract the winning years for ONE team?

Winston
Sujath Mohammed
Ranch Hand

Joined: Apr 17, 2013
Posts: 31
Winston Gutkowski wrote:
Sujath Mohammed wrote:So i had one team i would write a code like this:...

That's not what I asked. Obviously, you can simply write a custom program for an individual team.

One more time: given your existing arrays, how would you extract the winning years for ONE team?

Winston

Is this what you asking for?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Sujath Mohammed wrote:Is this what you asking for?

That's more like it, except you don't really need all those print() statements.

Think about it functionally: given a team name, you need a list of the years it won. You can worry about how you're going to print it out afterwards.

Now, given that you know how to do it for one team, think about how you'd do it for multiple teams

(Hint: you're just repeating the same logic, so I'd strongly suggest putting it in a method).

Winston
Sujath Mohammed
Ranch Hand

Joined: Apr 17, 2013
Posts: 31
Winston Gutkowski wrote:
Sujath Mohammed wrote:Is this what you asking for?

That's more like it, except you don't really need all those print() statements.

Think about it functionally: given a team name, you need a list of the years it won. You can worry about how you're going to print it out afterwards.

Now, given that you know how to do it for one team, think about how you'd do it for multiple teams

(Hint: you're just repeating the same logic, so I'd strongly suggest putting it in a method).

Winston


I see where you getting at but in that code the team depends on what the user has entered since i'm new to java i can display the whole arrays but when it comes to parallel arrays
and sorting only one of them into alphabetical i really don't how to do it if you give me a example code i can work on that and try adjusting it to my program
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Sujath Mohammed wrote:I see where you getting at but in that code the team depends on what the user has entered since i'm new to java i can display the whole arrays but when it comes to parallel arrays
and sorting only one of them into alphabetical i really don't how to do it if you give me a example code i can work on that and try adjusting it to my program

I'd prefer that you work it out for yourself; you'll learn a lot more, and you'll be much happier when you do so, rather than just being given the answer.

One of the most important aspects of programming is learning how to break problems down into small pieces.
As I understand it, you need to list out teams in alphabetical sequence, along with the years they won the championship; and (and this is MOST important) you need to do it with the arrays you were given.

So: you've now worked out how to list those wins for a specific team.
How do you think you might apply that logic to a list of teams supplied in alphabetical order?

Winston
Sujath Mohammed
Ranch Hand

Joined: Apr 17, 2013
Posts: 31
Winston Gutkowski wrote:
Sujath Mohammed wrote:I see where you getting at but in that code the team depends on what the user has entered since i'm new to java i can display the whole arrays but when it comes to parallel arrays
and sorting only one of them into alphabetical i really don't how to do it if you give me a example code i can work on that and try adjusting it to my program

I'd prefer that you work it out for yourself; you'll learn a lot more, and you'll be much happier when you do so, rather than just being given the answer.

One of the most important aspects of programming is learning how to break problems down into small pieces.
As I understand it, you need to list out teams in alphabetical sequence, along with the years they won the championship; and (and this is MOST important) you need to do it with the arrays you were given.

So: you've now worked out how to list those wins for a specific team.
How do you think you might apply that logic to a list of teams supplied in alphabetical order?

Winston


I'm not telling you give me the answer i'm just asking if you link me or give a code something similar to what i need
Sujath Mohammed
Ranch Hand

Joined: Apr 17, 2013
Posts: 31
and one more thing you see i have put this code for my first case but when i enter something thats not in the array it repeats the word "invalid" which i don't want i want it to say valid only once and then ask the user to input a proper team
here's the code:
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Sujath Mohammed wrote:here's the code:...

Looks exactly like what you showed us before.

I'll give you a little start, but remember that this is only ONE way to do it:Winston
Sujath Mohammed
Ranch Hand

Joined: Apr 17, 2013
Posts: 31
will bubble work with parallel arrays
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Sujath Mohammed wrote:will bubble work with parallel arrays

If you mean a bubble sort: It will work with any array. But the fact is that you DON'T want to sort either of your parallel arrays; otherwise they won't be parallel any more. You need to sort copies of them.

My advice: StopCoding and write down what you think you need to do. You're overthinking this at the moment.

Winston
Sujath Mohammed
Ranch Hand

Joined: Apr 17, 2013
Posts: 31
Winston Gutkowski wrote:
Sujath Mohammed wrote:will bubble work with parallel arrays

If you mean a bubble sort: It will work with any array. But the fact is that you DON'T want to sort either of your parallel arrays; otherwise they won't be parallel any more. You need to sort copies of them.

My advice: StopCoding and write down what you think you need to do. You're overthinking this at the moment.

Winston


You are so right i am trying to get it completed by tomorrow because its due tomorrow and this sorting is doing my head
and that the least of my worries if you look at my main menu you can see i also need to sort this into a bar chart as well
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Sujath Mohammed wrote:You are so right i am trying to get it completed by tomorrow because its due tomorrow and this sorting is doing my head...

Then my advice still applies. You're not going to get it all done, so do what you can; and the most important part of that is making a plan of what you need to do. I can't imagine you're yet at a stage where they're going to fail you for the entire course.

And next time: start earlier.

Winston
Sujath Mohammed
Ranch Hand

Joined: Apr 17, 2013
Posts: 31
Winston Gutkowski wrote:
Sujath Mohammed wrote:You are so right i am trying to get it completed by tomorrow because its due tomorrow and this sorting is doing my head...

Then my advice still applies. You're not going to get it all done, so do what you can; and the most important part of that is making a plan of what you need to do. I can't imagine you're yet at a stage where they're going to fail you for the entire course.

And next time: start earlier.

Winston

Funny that i started working on this assignment a week ago
Sujath Mohammed
Ranch Hand

Joined: Apr 17, 2013
Posts: 31
alright so far i came with this code but as it puts into alphabetically it still doesn't update the array
whats is the wrong with the code
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Sujath Mohammed wrote:alright so far i came with this code but as it puts into alphabetically it still doesn't update the array

Hmmm. Where and how are you calling it? Show us that. Apart from not being a optimal bubble sort, I can't see much wrong with what you've written.

And furthermore: Were you asked to write a sort? Because, as has already been stated, you can use Arrays.sort(), which definitely works.

Winston
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
And, I am afraid, the incorrect indentation makes the code more difficult to read. Incorrect indentation will lose you marks, of course.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
Sujath Mohammed wrote: . . . whats is the wrong with the code . . .
I still think your principal problem is using parallel arrays rather than creating a Winner class.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to get my arrays alphabetically sorted?