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.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
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.
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.
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.
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