From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

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

I'm glad to have learn about sorting an array of cluster! I think the "elegant" solution would be to use Scan From String and Format Into String with the cluster sort.

 

puzzle sort-2.png

 

Ben64

Message 11 of 17
(2,159 Views)

@ben64 wrote:

I'm glad to have learn about sorting an array of cluster! I think the "elegant" solution would be to use Scan From String and Format Into String with the cluster sort.

 


To protect from losing certain parts of the original elements (extra spaces, words after the second number, hidden characters, etc. i.e. anything that is not part of the two numbers or the special delimiter), I would just get the sort key and rebuild from the original array, e.g. as follows. Seems safer.

 

 

Message 12 of 17
(2,141 Views)

@altenbach wrote:
To protect from losing certain parts of the original elements (extra spaces, words after the second number, hidden characters, etc. i.e. anything that is not part of the two numbers or the special delimiter), I would just get the sort key and rebuild from the original array, e.g. as follows. Seems safer.

 


Here's what I meant by that (adding some extra characters). Reformatting can always introduce some additional scrubbing and it is not always desirable (sometimes it is, though :D). My code retains all original content while still sorting by the two numerics.

 

 

Message 13 of 17
(2,133 Views)

@ben64 wrote:

I'm glad to have learn about sorting an array of cluster! I think the "elegant" solution would be to use Scan From String and Format Into String with the cluster sort.

 

puzzle sort-2.png

 

Ben64


I honestly didn't know one could sort an array of cluster.  So I have definitely learned something from this exercise.  Thank you all!

0 Kudos
Message 14 of 17
(2,125 Views)

@DailyDose wrote:

@ben64 wrote:

I'm glad to have learn about sorting an array of cluster! I think the "elegant" solution would be to use Scan From String and Format Into String with the cluster sort.

 

puzzle sort-2.png

 

Ben64


I honestly didn't know one could sort an array of cluster.  So I have definitely learned something from this exercise.  Thank you all!


I hold back sometimes not wanting to repeat myself, but it seems it was good that I chimed in with the cluster sort. At least Y'all did not have to pay the price I paid to learn about that.

 

 

Spoiler

The price I had to pay...

 

Years ago the one of the lead architects of labVIEW, "Greg McKaskle" used to answer question here on teh forums. That was prior to LV 7.0. He also would do some early webinars on LabVIEw called "The Good, The bad, and The Ugly". He needed foder to review for the first show and asked for volunteers to submit code that he could review in the show.

 

I submitted some code I wrote that offered a Picture control with a Pie Chart that represented the amount of space allocated to folders on a disk drive and allowed a user to click on a slice of the pie to drill down into sub-folders. It was needed becuase my son was using the same machine I was using at home and my disk space was diappearing.

 

So I was hoping to get some words about how clever the application was blah blah blah. But to my horror, the code was used as "Ugly"! The reason? Not becasue the code was ungly but becuase I had developed my own "Bubble sort" to determine how to arrange the pie pieces. It was durring that show that I learned about the Cluster sort.

 

It was a high price to pay but I have made the best of it since then.

 

 

 

Enjoy!

 

Ben

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

@altenbach wrote:

@altenbach wrote:
To protect from losing certain parts of the original elements ...
My code retains all original content while still sorting by the two numerics.

 

 


Forgive my dear wizard but you failed to expound on the truly powerful aspect of using your approach of sorting the keys and then re-ordering the original array.

 

When face with very large data sets in a table for example, it is common that customers may want the information to be sorted on different collumns...maybe last name by defualt but want to sort based on "hire date" for example.

 

Doing the sort on the entire table could tax memory and CPU but using the inex appraoch you demonstrated, the keys and their index can be sorted and then the entire large data structure be re-ordered "in-place" using the indexes.

 

When faced with sorting in my applications, I have often used the apporach your demonstrated.

 

Thank you Christian!

 

Ben

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

Re: sorting 1D array of clusters

 

For those not previously familiar with this, it's important to note that the order of elements within the cluster is key to how it behaves.  It compares elements to determine order starting with element #0.  In case of a tie, it moves on to element #1, and so on.

 

I recently learned that the Array Max & Min primitive works similarly on a cluster array.  I was working on a scheduling algorithm, and by making sure the "scheduled time" was element #0 in the cluster, I could just perform Array Max & Min and know that the Min result give me the earliest scheduled item in the array, i.e., the next item to be concerned with.  

   Since my list would rarely be longer than a handful of elements, this approach seemed neater than the extra work involved with needing to maintain the array in sorted order.  (For a very large array and lots of insertions, it'd be worth considering maintaining sorted order and using a binary search to determine insertion points).

 

Digression over.

 

 

-Kevin P

CAUTION! New LabVIEW adopters -- it's too late for me, but you *can* save yourself. The new subscription policy for LabVIEW puts NI's hand in your wallet for the rest of your working life. Are you sure you're *that* dedicated to LabVIEW? (Summary of my reasons in this post, part of a voluminous thread of mostly complaints starting here).
Message 17 of 17
(2,055 Views)