LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Sorting 1D Array Discussion - How to Sort the following? Fun "Puzzle"

Hello LabVIEW community.  We all love puzzles, yes?  Well here's one for you as I was working on a sorting function cuz this one required me to do some serious thinking because of all the different scenarios that could take place.  So I'd be curious how everyone else would do this.  I'm always impressed by some of the much simpler ways some of y'all come up with.  I'll post a picture of the issue and the VI (in LV2015).  The goal is to get the data on the left sorted to look like the data on the right.  Good Luck.  Smiley Happy  (not terribly hard, but still fun I thought)

Sorting Puzzle.JPG

0 Kudos
Message 1 of 17
(4,018 Views)

http://www.ni.com/example/25012/en/

 

I'd use this, because I'm lazy and don't like re-inventing the wheel

Message 2 of 17
(3,988 Views)

Assuming the first number is less than 10000 you can do something like that:

-multiply the first number by 1000 and add the second one

-sort this 1D array

-recreate  the string array

 

puzzle sort.png

You can use a bigger scale factor if necessary. Of course it is not universal and would have like to find a 1 or no loop solution but this seems to be an easy way to do this.

 

Ben64

Message 3 of 17
(3,976 Views)

1) Use "match patern" with a patern of "+/-" in a loop to break up each string.

2) Bundle then into a cluster with the "Before substring" in the first element and the "After substring" in a second cluster element.

Note:

Verify the cluster order such that the first part is cluster element 0 and the second string is element 1.

3) build an array using an autoindiexing output tunnel.

4) Pass the cluster to sort 1-d array

5) Take the sorted array and put the two parts back together again with " +/- " inbetween.

 

Done,

 

Ben

Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
Message 4 of 17
(3,970 Views)

@OEM_Dev wrote:

http://www.ni.com/example/25012/en/

 

I'd use this, because I'm lazy and don't like re-inventing the wheel



Well... huh.  I mean, I don't mind a challenge and proving to myself that I can do it.  But yeah... that would have been nice to know that existed originally.  It's definitely more complicated than what I did, but it also achieves every possible (least what I can think of) scenario.  My solution is for my specific case and probably shouldn't be used for any and every string.  Oh well...  still fun... right? Smiley Indifferent

0 Kudos
Message 5 of 17
(3,964 Views)

What is fun for me is remebering I read about an elegant solution when reading others post on this forum.

 

Smiley Wink

 

Ben

Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
0 Kudos
Message 6 of 17
(3,955 Views)

*Edit: I posted before I had read any replies to the post, I see now that I am only repeating Ben's solution...*

 

Fun exercise! It took a while for me to figure out an "acceptable" solution. I ended up doing this:

(assuming only standard functions is to be used, of course)

sorting.png

 

 

Message 7 of 17
(3,953 Views)

@ben64 wrote:

Assuming the first number is less than 10000 you can do something like that:

-multiply the first number by 1000 and add the second one

-sort this 1D array

-recreate  the string array

 

puzzle sort.png

You can use a bigger scale factor if necessary. Of course it is not universal and would have like to find a 1 or no loop solution but this seems to be an easy way to do this.

 

Ben64


I did similar.  But I was thinking that because I don't know what the maximum number is nor do I know what the tolearances are going to be  I risk a "duplicate" that won't be sorted correctly.

0 Kudos
Message 8 of 17
(3,947 Views)

@perhult wrote:

Fun exercise! It took a while for me to figure out an "acceptable" solution. I ended up doing this:

(assuming only standard functions is to be used, of course)

sorting.png

 

 


Oh, very nice!  Kudos awarded.  Also... I think this is what Ben was talking about.

 

Edit: I saw your edit.  And yep.  But it's cool.  I know it was original.

0 Kudos
Message 9 of 17
(3,943 Views)
@DailyDose wrote:

@OEM_Dev wrote:

http://www.ni.com/example/25012/en/

 

I'd use this, because I'm lazy and don't like re-inventing the wheel



Well... huh.  I mean, I don't mind a challenge and proving to myself that I can do it.  But yeah... that would have been nice to know that existed originally.  It's definitely more complicated than what I did, but it also achieves every possible (least what I can think of) scenario.  My solution is for my specific case and probably shouldn't be used for any and every string.  Oh well...  still fun... right? Smiley Indifferent


I had to do a similar sort in a project I did recently which I why I knew this existed off the top of my head Smiley LOL definitely saved me a lot of time. I try to search the forums before starting something complicated because this isn't the first time I've found a solution thats a lot more elegant than mine on here.

0 Kudos
Message 10 of 17
(3,941 Views)