08-17-2024 02:45 PM
Hi everyone,
I’m working on a project where I need to sort an array of clothing items in ascending order based on a specific sequence. I have three types of clothing items: shoes, pants, and shirt. I want to arrange them in the following order: shoes, pants, and then shirt.
For example:
- Given the 1D string array: pants, shoes, shirt
The output should be: shoes, pants, shirt
- Given the array: pants, pants, shirt, shoes
The output should be: shoes, pants, pants, shirt
- Given the array: shoes, pants, pants, shirt, shoes
The output should be: shoes, shoes, pants, pants, shirt
I’m looking for a way to sort the array such that the items are ordered according to the specified sequence.
thank you!
Solved! Go to Solution.
08-17-2024 03:29 PM
Seems simple enough. What have you tried?
08-17-2024 03:29 PM
This is how I would do it.
08-17-2024 03:41 PM - edited 08-17-2024 03:52 PM
@ZYOng wrote:
This is how I would do it.
and this is how I would do it. 😄
Of course if there could be unlisted items in the array or if we need to ignore upper/lower case, etc. a little bit more code is needed once we know the extra behavior needed.
Comments for the earlier solution posted:
Three stacked structure, two shift registers, two different output tunnels (conditional indexing on a while loop and a concatenating tunnel on a FOR loop), delete from array in a tight loop (big no-no!) all seems a bit convoluted for such a simple problem. 😄
08-17-2024 03:42 PM
I did it in key-value form and in a much more complicated way. It definitely makes a lot more sense when I see the solution
08-17-2024 03:49 PM
Thanks! its great
08-17-2024 07:19 PM
08-20-2024 07:55 AM
Just for fun. (i know it can be improved with Maps, but i'm still unused to those)
08-20-2024 09:52 AM
Here's a simple "Count and Concatenate" solution that doesn't actually sort anything.