• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Advanced string array sorting code

 
Greenhorn
Posts: 4
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I have a string array in Java. I need to sort it by the number of character "a"s within the word (decreasing order). If some words contain same number of character "a", then I need to sort those words by their lengths(decreasing order). And if the length is same, then alphabetically.

Example array:

["aaaasd","a","aab","aaaabcd","ef","cssssssd","fdz" ,"kf","zc","lklklklklklk","l"]

needs to be sorted like:

["aaaabcd","aaaasd","aab","a","lklklklklklk","cssss ssd","fdz","ef","kf","zc","l"]

I am a beginner in Java and I don't have any idea how to make. Anyone can help?

Thanks in advance
 
Bartender
Posts: 1845
10
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You must have SOME idea of how to start.

How about this. Given a String, can you tell how many times the letter 'a' occurs in it?
Write some code (preferably a method) to do that.
 
Telmessos Kahin
Greenhorn
Posts: 4
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can loop the array to find the number of "a"s in the words. But I don't know how to continue. Plus I am a newbie in Java I don't know if this is a good way to do in Java and if there are some built-in functions which I can use to solve this problem. That's why I am asking here to get some practical ideas about sorting.
 
Marshal
Posts: 76811
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

That is an unusual sorting method, so you won't find any ready‑made methods. There is something useful about object ordering in the Java™ Tutorials.
 
Telmessos Kahin
Greenhorn
Posts: 4
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've already read the documentation and tried to understand how I can use comparable, made some searches on the different forums but haven't found a solution for my problem. That's why I am asking for help here telling the exact problem that I have to solve.
 
Stefan Evans
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using comparable sounds like a great solution to me.

Can you show us what you have so far?
What does your solution for counting the number of 'a's in a word look like?
 
Telmessos Kahin
Greenhorn
Posts: 4
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I found the solution. Posting here if someone else needs in the future.

Thanks

 
Campbell Ritchie
Marshal
Posts: 76811
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well done, but you are repeating code there. You can improve that by removing the repetition.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Telmessos Kahin wrote:I found the solution. Posting here if someone else needs in the future.


Very well done! (I hope it's you own work )

And thanks for posting it too. Have a cow.

A small nitpick: String.compareTo() already checks lengths, so I doubt that you have to.

Winston
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Telmessos Kahin wrote:I found the solution...


Another minor point: Comparators are useful things, and I often find myself re-using ones I wrote earlier; but in order to do that you have to make them a named class.

How about breaking out that anonymous class and creating a FrequencyComparator that can compare the occurrences of ANY character in a String, not just an 'a'?

HIH

Winston
 
Bras cause cancer. And tiny ads:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic