Win a copy of Terraform in Action this week in the Cloud forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Code optimization and good practices

 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a array containing let's say pu_1, iu_3, tu_5, ru_1, tu_3. my order of preference is ascending order, if the same number appears, then I need to order it in the preference of ru, pu, tu, iu.

So, the final output should be ru_1, pu_1, tu_3, iu_3, tu_5.

Please suggest an optimized way to do it using any data structure and the least number of steps.

Thanks
Jacob
 
lowercase baba
Posts: 13014
66
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the best practice would be to write it in the simplest, cleanest way you can to get the job done, and not worry about "the least number of steps" - especially since 'steps' do not equate to 'time'. depending on what the steps are, doing it in 10 steps could be faster than doing it in 4.

 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And the cleanest way, I think, would be to write a Comparator that implements that logic, and then call Arrays.sort using that Comparator.
 
Marshal
Posts: 74387
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jacob Sonia wrote: . . . pu_1, iu_3, tu_5, ru_1, tu_3. . . . order it in the preference of ru, pu, tu, iu.

So, the final output should be ru_1, pu_1, tu_3, iu_3, tu_5. . . .

That looks like sorting both on the number and on the first letter, if the middle is always u_. You can create a Comparator which uses the number and the first letter, or you can sort twice, first on the number, using a stable sorting algorithm. You should look in the API documentation whether the sorting method you were suggested earlier is stable or not.
 
Jacob Sonia
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried assigning each iu, pu etc values like .1, .2, .3 etc depending on the order of preference.
I then iterated over the original list and for each of the elements, I extracted the index and then assigned values to it as per the index and the value it has, for instance suppose the index for iu_1 is 1, and let's say Its preference is .2, I assigned it's value as 1.2, and then added to the treemap, I did that for all the elements, since treemap sorts the elements, I got an ordered list.

Now, is this a good approach.
 
Campbell Ritchie
Marshal
Posts: 74387
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
TreeMap or TreeSet? You won't get a List from either, but a sorted set, I would have thought. The entries are sorted by their value, whereas most Lists retain insertion order. Also Lists retain duplicates and sets discard duplicates.
Yes, a TreeSet combined with a decent Comparator is a good way to sort those values; you can even retain the order in the original array. But I thought you were not allowed to use a data structure; that's what you said in your first post.
 
Jacob Sonia
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I had told using any datastructure, i wanted to use TreeMap because i thought it was an easy way and then take all the values and have them in a list.

But somehow, i am not comfortable with the approach, so i want to get some recommendations on the approach.
 
Campbell Ritchie
Marshal
Posts: 74387
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, TreeMap is not a List. Since Maps don't allow duplicate keys, and Lists allow duplicates, you are liable to lose data if there are any duplicates. I would suggest you read the API documentation for these interfaces: List, Set and Map. Also the descriptions in the Java™ Tutorials. You need to understand the differences between Lists, Maps, Queues, Sets etc., before using them.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic