• Post Reply Bookmark Topic Watch Topic
  • New Topic

Code optimization and good practices  RSS feed

 
Jacob Sonia
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • 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
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • 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.

 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • 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: 183
  • Mark post as helpful
  • send pies
  • 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: 56576
172
  • Mark post as helpful
  • send pies
  • 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: 183
  • Mark post as helpful
  • send pies
  • 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: 56576
172
  • Mark post as helpful
  • send pies
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!