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: 

Arrange Array in ascending order by date


@Grugh_Mike wrote:

is'nt there any easier method to sort a time array ?


The easiest way is probably to generate the date strings so that alphabetical order = chronological order (YYYY.MM.DD).  Other than that, what is so hard about this:

 

SortDateStrings.png

 

Lower version is there in case you are bothered by your dates getting 0 padded.

Message 11 of 25
(3,418 Views)

P@Anand - why so many unnecessary type conversions?  You can sort an array of timestamps directly, no need to convert to and from DBLs.

 

However, I think an even easier approach is shown below: scan the date directly into a double and sort on that.  Here are two alternatives for getting the result as a an array of sorted strings.

sort array of string dates.png

sort array of string dates2.png

Message 12 of 25
(3,417 Views)

@nathand wrote:

P@Anand - why so many unnecessary type conversions?  You can sort an array of timestamps directly, no need to convert to and from DBLs.

 


"Unnecessary" type conversions?? You're doing the exact same thing as P@Anand but in a Rube Goldberg way. I can't imagine how using regex is simpler and easier than using primitives. P@Anand's is the way I would do it. So much simpler and straightforward.

PaulG.

LabVIEW versions 5.0 - 2020

“All programmers are optimists”
― Frederick P. Brooks Jr.
Message 13 of 25
(3,397 Views)

@PaulG. wrote:

"Unnecessary" type conversions?? You're doing the exact same thing as P@Anand but in a Rube Goldberg way. I can't imagine how using regex is simpler and easier than using primitives. P@Anand's is the way I would do it. So much simpler and straightforward.


Paul - What I was saying is that P@Anand's conversion to and from DBL is unnecessary, because the Sort 1D Array function will accept an array of timestamps.  The poster asked about sorting an array of strings containing dates, so P@Anand's solution is not sufficient on its own.  My (and Darin's) approach uses a standard format specifier, not a regular expression, and answers the question that was asked with a minimal number of standard functions.

0 Kudos
Message 14 of 25
(3,387 Views)

nathand wrote:

Paul - What I was saying is that P@Anand's conversion to and from DBL is unnecessary, because the Sort 1D Array function will accept an array of timestamps.  The poster asked about sorting an array of strings containing dates, so P@Anand's solution is not sufficient on its own.  My (and Darin's) approach uses a standard format specifier, not a regular expression, and answers the question that was asked with a minimal number of standard functions.


I do know sorting time stamps but I doubted it thats why I haven't suggested. I see nowhere the poster has mentioned that the date is in string and simply by the way he has given the date I didn't take that as a string so gave a generic answer.

-----

The best solution is the one you find it by yourself
0 Kudos
Message 15 of 25
(3,364 Views)

P@Anand wrote:

I do know sorting time stamps but I doubted it thats why I haven't suggested. I see nowhere the poster has mentioned that the date is in string and simply by the way he has given the date I didn't take that as a string so gave a generic answer.


Sorry, yes, you're right, someone else said they assumed it was strings and the original poster didn't say otherwise in their follow-up message.

 

It does work to sort time stamps - I just wrote some code that took advantage of that last week to find the most recently modified file among several folders.  I think that's an easier starting point than a date/time record, and saves an additional conversion as well (in addition to dropping the doubles).

0 Kudos
Message 16 of 25
(3,353 Views)

No sorting the time array doesn't seem to work good. You can try Timestamp sorting method and DBL sorting method with a folder containing large files and check the difference. The DBL sorting method gave me exact sort which I did using my windows explorer sort by date modified. (Please correct me if I am worng)

-----

The best solution is the one you find it by yourself
0 Kudos
Message 17 of 25
(3,334 Views)

P@Anand wrote:

No sorting the time array doesn't seem to work good. You can try Timestamp sorting method and DBL sorting method with a folder containing large files and check the difference. The DBL sorting method gave me exact sort which I did using my windows explorer sort by date modified. (Please correct me if I am worng)


Here's a snippet that compares the results of sorting modification dates as timestamps versus as DBLs.  In the folders I checked, both produce identical results.  If you can find a mismatch, could you post a snippet or VI containing constants for those timestamps?  I think that would be a bug in LabVIEW.
sort double versus timestamp.png

Message 18 of 25
(3,324 Views)

Its my mistake the bug is with my logic in the timestamp sorting. I have swapped the inputs for the search array in the 2nd for loop.

-----

The best solution is the one you find it by yourself
0 Kudos
Message 19 of 25
(3,307 Views)

I need to sort the date time in array . pls help as i have tryime many ways and it still not sort according to date/time instead of softing the first number. Pls see attached picture.

0 Kudos
Message 20 of 25
(2,779 Views)