01-10-2017 01:25 PM
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. (not terribly hard, but still fun I thought)
01-10-2017 02:20 PM
http://www.ni.com/example/25012/en/
I'd use this, because I'm lazy and don't like re-inventing the wheel
01-10-2017 02:32 PM
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
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
01-10-2017 02:38 PM
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
01-10-2017 02:41 PM - edited 01-10-2017 02:43 PM
@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?
01-10-2017 02:43 PM
What is fun for me is remebering I read about an elegant solution when reading others post on this forum.
Ben
01-10-2017 02:44 PM - edited 01-10-2017 02:48 PM
01-10-2017 02:46 PM
@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
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.
01-10-2017 02:48 PM - edited 01-10-2017 02:52 PM
@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)
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.
01-10-2017 02:50 PM
@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?
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 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.