Example Program Drafts

cancel
Showing results for 
Search instead for 
Did you mean: 

Sort 2D String Array

Readme file for Sort 2D String Array VI

Description



The Sort 2D String Array VI performs a multi-column sort on a 2D array of strings that contains both plain text strings, Hex strings and strings representing real numbers. The user can specify the column sort order.

Run the included example VI to see how you can use this functionality in your applications.



Usage Scenarios



You store test parameters in a file consisting of one record for each named test.

The records consist of the following elements represented as columns in the data:

  • Test Name

  • CurrentComp

  • CurrRange

  • SourceVoltage

  • VoltRange

  • Amplitude

  • Width

  • ActivePressure

  • Temperature


Your test requirements call for running various tests at different temperatures. Each time that the temperature is changed, a 20 minute ramp-up and temperature stabilization period is required .

To minimize test time, it is desirable to perform all of the tests with the same temperature requirement before moving to the next temperature set point.

The Sort 2D String Array VI enables you to accomplish this by providing the ability to sort a 2D array of strings that contains both plain text strings and strings representing real numbers. In this case, the Sort 2D String Array VI enables you to sort your test parameters first on the temperature and then on the test name.

For example, the Sort 2D String Array VI enables you to sort the data in the following table first by Amplitude and then by Test Name:

Test Name

CurrentComp

CurrRange

SourceVoltage

VoltRange

Amplitude

Width

ActivePressure

Temperature

Battery000

6.693E-1

2.06E+0

5.0

100.00

3.957118784

10.00

True

4.00

Battery001

1.950E+0

2.51E+0

9.5

5.00

0.934304207

40.00

False

26.00

Battery002

3.569E+0

6.40E-1

3.2

10.00

-2.327802279

30.00

True

22.00

Battery003

2.501E+0

2.51E+0

9.5

10.00

2.035078922

40.00

False

28.00

Battery004

1.180E+0

2.33E+0

5.0

20.00

3.257387404

20.00

True

11.00


The result of the sort is:

Amplitude

Test Name

CurrentComp

CurrRange

SourceVoltage

VoltRange

Width

ActivePressure

Temperature

-2.327802279

Battery002

3.569E+0

6.40E-1

3.2

10.00

30.00

True

22.00

0.934304207

Battery001

1.950E+0

2.51E+0

9.5

5.00

40.00

False

26.00

2.035078922

Battery003

2.501E+0

2.51E+0

9.5

10.00

40.00

False

28.00

3.257387404

Battery004

1.180E+0

2.33E+0

5.0

20.00

20.00

True

11.00

3.957118784

Battery000

6.693E-1

2.06E+0

5.0

100.00

10.00

True

4.00

Run the included example VI to see how it works.

Application Software



LabVIEW 2010 or higher

Driver Software



None.

Concepts Illustrated



The Sort 2D String Array VI illustrates the following concepts:

  • A technique for sorting a 2D array of strings that contains both plain text strings and strings representing real numbers.

  • Using the Busy Indicator VI to provide an indication to the user that the system is busy.

  • Using the Selection Dialog VI to enable the user to select items from a list and manage the selected items.

Description of Operation



The Sort 2D String Array VI performs a multi-column sort on a 2D array of strings that contains both plain text strings and strings representing real numbers. The user can specify the column sort order.

Screenshots



Sort 2D String Array.png

Notice that in the screenshot above, we have sorted a 2D array of strings that contains both plain text strings and strings representing real numbers. In this case, we have sorted on a single column (Amplitude), but the Sort 2D Array VI provides the ability to sort on multiple columns. The ability to sort a 2D array of strings that contains both plain text strings and strings representing real numbers is a key challenge faced by anyone who works with string data.

Extracting the files



The Sort 2D String Array VI is supplied as a zip archive containing all required items.

Additional Information



Additional information related to the use and operation of the Sort 2D String Array VI may be accessed via:

  • The block diagrams of all included VIs

  • The documentation within all included VIs

Change Control


11-11-2008
Modified the Sort 2D String Array.vi so that the output array is always the same size as the input array.

11-10-2008
Modified the Sort 2D String Array - String or Number.vi to scan for dates formatted as: nn<separator>nn<separator>nnnn or nnnn<separator>nn<separator>nn.

Modified the Sort 2D String Array - Sort Data.vi so that no rows are dropped when the "Input Array Contains Header Row? (T)" input is false.

Modified the Sort 2D String Array.vi so that the input array is passed to the output array unchanged if the user cancels the sort operation.

Modified the Sort 2D String Array.vi so that the array without headers is passed to the output when the "Input Array Contains Header Row? (T)" input is false.

11-31-2010

Updated code for LabVIEW 2010.

03-11-2012

Modified code in response to comments regarding handling of Hex strings and the issue of data values that are exactly equal to -1.

Developed by



Mark Ridgley

Mark.Ridgley_LabVIEW.Developer@comcast.net
Comments
Member Ancle
Member

When I try this vi, I found there is a bug. Please see the attached image. Thank you!

Member Mark.Ridgley Member
Member

Hello, Ancle

The behavior that you are observing is by design.

The Sort 2D Array - String or Number VI examines each character in "Input String" individually and determines if that character is a hex digit ranging from 0 through 9, A through F, or a through f.

If all characters in "Input String" are hex digits ranging from 0 through 9, A through F, or a through f, then the "Is Num" output is True as shown in the image that you posted.

In the test case that you posted, each character in the string "ABCDEF" is a hex digit ranging from A through F, so "Is Num" is True. If you add a "G" anywhere within that string, then "Is Num" will be False.

In your usage of this VI, does the data that you are trying to sort normally contain strings where all of the characters in the string range from 0 through 9, A through F, or a through f that should not be interpreted as hex numbers?

Thanks for posting.

Member Ancle
Member

Hello Mark,

Sorry, I should say " I am not sure it's bug or not?"

In my application, I have a column, strings are"A,B,C,D". When I sort the 2D array. This column shows all "0". How can I solve this problem(can show "A,B,C,D")? Thank you!

Best Regards

Ancle

Member thatsme
Member

Sorting a 2D table on one column is quite easy to program, but to sort also for other columns secondly is nicely implemented in this .vi.

Thanks a lot,

me

Member smartcookie
Member

I also had this problem: I had columns with "hex-like" strings in them eg CC1,CC2,CC3 etc. Normally the sorted result will contain zeros for these strings. You can however prevent the VI from interpreting a "hex-like" string as a number by enclosing the string in quotes.

Normal behaviour (sorting on columns 0,1,2):

Sort 2D String Array_1.jpg

But enclose the hex like stings in quotes and the intended result is displayed:

Sort 2D String Array 2.jpg

Member Stephen_@_HII-Ingalls
Member

Awesome.  Just what I was looking for.  I did notice a potential bug in the subVI "Sort 2D String Array - StringTransfrom (Pre Sort).vi, where the In Range and Coerce funtion is being used.  It includes the lower limit of -1.  If you have data in a column that is exactly equal to -1, it gets converted to -2 after sorting.  I unchecked the Include lower limit property (i.e. don't include -1) and it worked perfectly.  My assumption is that the purpose is to check if a value is between 0 and -1 (i.e. -1 < x < 0) for formating purposes.  Not sure if I've caused other problems, but I haven't seen any just yet.

Thanks,

Stephen

Member Mark.Ridgley Member
Member

smartcookie and Ancle,

I've modified this example code to address handling of Hex values. Hex values are sorted properly provided that the data values are preceded by the radix specifier string "0x" and all values are the same width.

Thanks for helping to make this code better.

Mark Ridgley

Member Mark.Ridgley Member
Member

Stephen,

I've modified this example code to address the issue of data values that are exactly equal to -1. I implemented the change as you described.

Thanks for taking the time to let me know about this issue.

Mark Ridgley

Member Ancle
Member

Dear Mark,

Thank you. I have customer data. The mobile phone format is 09xxxxxxxx, after I sorted the data became 9xxxxxxxx. And, the phone number format is 02-xxxxxxxx. After I sorted the data became 2. How can I solve this problem? Thanks again.

Best Regards,

Ancle

Member MarcusSch
Member

Hi,

first of all I thank you for this nice VI.

But I think I found a bug (perhaps it is fixed already as I use an older version):

Lower case umlauts become uppercase umlauts (ü -> Ü)

Capital and lower case umlauts both have the same lexical class 0. I think this is the problem.

Best regards,

Marcus

Member TimsTools
Member

This fits a related audience.  If you are in need of a 2D Multi Column Sort with Ascending/Descending capability, Polymorphic, try...

https://decibel.ni.com/content/docs/DOC-48871

Contributors