• 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

sorting last name

 
Greenhorn
Posts: 26
Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I can sort it based by first name .. but i cant sort it based last name (last here means second)
Example :
name 2 : aaa bbb
name 3 : bbb ddd
name 4 : ccc aaa

id i want to sort it based by last name, it'll be:

ccc aaa
aaa bbb
bbb ddd

Can you fixed it?
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to use HashMap concept for that. you can't do sorting with last names in your code. Yeah you can do it but you need to write a bit complex logic for that. You can use String class's substring() method to get last names and then you can make a new array of last names and then you can sort that array writing logic
 
Gabrielle Linkherz
Greenhorn
Posts: 26
Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sankalp Bhagat wrote:You need to use HashMap concept for that. you can't do sorting with last names in your code. Yeah you can do it but you need to write a bit complex logic for that. You can use String class's substring() method to get last names and then you can make a new array of last names and then you can sort that array writing logic



And... How to do that?

Sorry .. i'm still a noob
 
Gabrielle Linkherz
Greenhorn
Posts: 26
Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sankalp Bhagat wrote:You need to use HashMap concept for that. you can't do sorting with last names in your code. Yeah you can do it but you need to write a bit complex logic for that. You can use String class's substring() method to get last names and then you can make a new array of last names and then you can sort that array writing logic



And, if i input
aaaa bbbbbbbbb
cds aaaaa

How can i use substring, when i don't even know the length and position of last names
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use String.indexOf for that.
Or, you can use String.split().
 
Sankalp Bhagat
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You know why you were missing "1 Name : " because of that "nextLine()" method of Scanner class.
you need to use BufferedReader class instead of Scanner class.

 
Sankalp Bhagat
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To clear your concept, I've given this reference code.

But remember, writing code inside main() method is not recommended. Always write another method for your required functionality and call that method from main() method.

and taking last names from the String and making its array can be doomed to failure if the input is given wrong. So this approach even is not recommended.

So you learn Collections concept from this url

https://docs.oracle.com/javase/tutorial/collections/intro/
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sankalp Bhagat wrote:You need to use HashMap concept for that. you can't do sorting with last names in your code. . . .

That is quite mistaken, I am afraid. You do not need a Map, and the logic for sorting is quite simple.
 
Sankalp Bhagat
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Sankalp Bhagat wrote:You need to use HashMap concept for that. you can't do sorting with last names in your code. . . .

That is quite mistaken, I am afraid. You do not need a Map, and the logic for sorting is quite simple.



Sir, Gabrielle is taking first name and last name in a single String. that's why I thought HashMap would be better. because what if the user enters two spaces or if the name is given as "Campbell" only then it can't find space index then program fails.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paweł is right. Use String#split. Pass "\\s+" as its regex argument. That is a regular expression which means any number of whitespace characters greater than 0. Start by splitting an input line and see what you get. Look up String#split() and see what it returns. Work out what you are going to do if there is no space in the input line.

Also find out about this other String method which might give you an alternative way to do things.

Look in the Java™ Tutorials to find out about collections. Note the section about object ordering which will tell you about sorting. I think you are going to require a Name class and you sort a Name[].
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sankalp Bhagat wrote:. . .
you need to use BufferedReader class instead of Scanner class.

That is incorrect. You can use Scanner. You only need to learn how to use nextLine, which you can find from the link I posted yesterday. You also do not have to worry about checked Exceptions.

That is bad design, using parallel arrays. Very non‑object‑loriented code, and prone to errors if you sort things.

Completely unnecessary local variable.

Both those variables are redundant. You can write the code without either.

That is incorrect use of the for‑each loop. You should use a different sort of loop there. A for‑each loop is not intended for altering the contents of an array, which is why you have that awkward syntax with k.

You are also writing very unoptimised code because you have two loops when only one loop is needed. You also don't have correct code for finding the last name.
 
Sankalp Bhagat
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Sankalp Bhagat wrote:. . .
you need to use BufferedReader class instead of Scanner class.

That is incorrect. You can use Scanner. You only need to learn how to use nextLine, which you can find from the link I posted yesterday. You also do not have to worry about checked Exceptions.

That is bad design, using parallel arrays. Very non‑object‑loriented code, and prone to errors if you sort things.

Completely unnecessary local variable.

Both those variables are redundant. You can write the code without either.

That is incorrect use of the for‑each loop. You should use a different sort of loop there. A for‑each loop is not intended for altering the contents of an array, which is why you have that awkward syntax with k.

You are also writing very unoptimised code because you have two loops when only one loop is needed. You also don't have correct code for finding the last name.



Thank you Sir. I will try to correct my coding style and will try to write optimized code.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sankalp Bhagat wrote:. . .
Thank you Sir. I will try to correct my coding style and will try to write optimized code.

We would like to see you writing correct code first. If I am paying money into the bank I do not mind whether it take 10 seconds or 20 seconds to get in. I do worry whether it gets to the right account.
 
reply
    Bookmark Topic Watch Topic
  • New Topic