Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sort array by date substring

Status
Not open for further replies.

GessWurker1

Technical User
Aug 17, 2019
5
US
In a web app for which I'm responsible, I've been asked to sort contents of a form field by date descending using the date that starts each list item. I'm supposed to do this either onBlur or perhaps onSave because users are entering stuff willy-nilly. In any event, though I can very easily sort items alphabetically using javascript's array sort() method, sorting by date substring descending is whole 'nuther kettle of fish. My head is spinning trying to find a way to do this. 'Tis beyond me.

Here's an example of what I'd need to sort by date descending:

•01/21/2013 - something happened; something else happened on 01/30/2013.
•04/02/2014 - John J. buys a new car
•02/25/2014 - We went to a movie. It was awesome, but a better one comes out on 12/31/2019.
•05/21/2012 - Let's keep sorting things.


Let's say the above is all included in a form field with id=myActivities. I'd like to, onBlur, get this:

•04/02/2014 - John J. buys a new car
•02/25/2014 - We went to a movie. It was awesome, but a better one comes out on 12/31/2019.
•01/21/2013 - something happened; something else happened on 01/30/2013.
•05/21/2012 - Let's keep sorting things.

Any brilliant sorters out there? I'm at your mercy.

By the way, in a windows app I can use to edit/update existing content, I use the (slightly proprietary looking) jscript code below. It works great, but it's simple alphabetical sorting. Nothing to do with substring dates.

****************************************

var sortBox = new Array("Events");

function onRecordSave()
{
var i;
var es = Application.entrySeparator;
var fieldBox; // box object
var fieldString; // contents of the box
var entryArray; // array of field entries

// This loop will sort the entries in each box listed
// above, individually.
for (i = 0; i < sortBox.length; i++)
{
fieldBox = Form.boxes(sortBox);
if (fieldBox) // ensure that the box exists
{
fieldString = fieldBox.content;
entryArray = fieldString.split(es);

// sort the entries using the JScript sort method
entryArray = entryArray.sort();
fieldString = entryArray.join(es);
fieldBox.content = fieldString;
}
}

}
 
Hi

The [tt]Array.sort()[/tt] method accepts a callback function as parameter to use it as comparator.

So in your above code you only need to change that call :
Code:
entryArray [teal]=[/teal] entryArray[teal].[/teal][COLOR=orange]sort[/color][teal]([/teal][highlight]compareDate[/highlight][teal]);[/teal]

Then implement the referred callback function :
JavaScript:
[b]function[/b] [COLOR=orange]compareDate[/color][teal]([/teal]a[teal],[/teal] b[teal])[/teal]
[teal]{[/teal]
  [b]var[/b] aDate [teal]=[/teal] [gray]/* code to extract the date part from parameter a */[/gray]
  [b]var[/b] bDate [teal]=[/teal] [gray]/* code to extract the date part from parameter b */[/gray]

  [b]if[/b] [teal]([/teal]aDate [teal]==[/teal] bDate[teal])[/teal]
    [b]return[/b] [purple]0[/purple]
  [b]else if[/b] [teal]([/teal]aDate [teal]<[/teal] bDate[teal])[/teal]
    [b]return[/b] [teal]-[/teal][purple]1[/purple]
  [b]else[/b]
    [b]return[/b] [purple]1[/purple]
[teal]}[/teal]


Feherke.
feherke.github.io
 
Thanks for the help. I think I'm getting closer, but I'm not quite sure how to share info between onRecordSave and compareDate. Here's what isn't working (I imagine because fieldString is empty?):


function compareDate(a, b)
{
var aDate = fieldString.match(rgx)[0];
var bDate = fieldString.match(rgx)[0];

if (aDate == bDate)
return 0
else if (aDate < bDate)
return -1
else
return 1
}

var sortBox = new Array("Events");

function onRecordSave()
{
var i;
var es = Application.entrySeparator;
var fieldBox; // box object
var fieldString; // contents of the box
var entryArray; // array of field entries
var rgx = /\d\d\/\d\d\/\d\d/;

// This loop will sort the entries in each box listed
// above, individually.
for (i = 0; i < sortBox.length; i++)
{
fieldBox = Form.boxes(sortBox);
if (fieldBox) // ensure that the box exists
{
fieldString = fieldBox.content;
entryArray = fieldString.split(es);

// sort the entries using the JScript sort method
entryArray = entryArray.sort(compareDate);
fieldString = entryArray.join(es);
fieldBox.content = fieldString;
}
}
}
 
I'm thinking I need to fully-embed the compare function in onRecordSave() so all the variables are available?
 
Hi

You have to extract the date parts from the received parameters :
JavaScript:
[b]function[/b] [COLOR=orange]compareDate[/color][teal]([/teal]a[teal],[/teal] b[teal])[/teal]
[teal]{[/teal]
  [b]var[/b] aDate [teal]=[/teal] a[teal].[/teal][COLOR=orange]match[/color][teal]([/teal][fuchsia]/\d{2}\/\d{2}\/(\d{4})/[/fuchsia][teal]).[/teal][COLOR=orange]reverse[/color][teal]().[/teal][COLOR=orange]join[/color][teal]()[/teal]
  [b]var[/b] bDate [teal]=[/teal] b[teal].[/teal][COLOR=orange]match[/color][teal]([/teal][fuchsia]/\d{2}\/\d{2}\/(\d{4})/[/fuchsia][teal]).[/teal][COLOR=orange]reverse[/color][teal]().[/teal][COLOR=orange]join[/color][teal]()[/teal]

  [b]if[/b] [teal]([/teal]aDate [teal]==[/teal] bDate[teal])[/teal]
    [b]return[/b] [purple]0[/purple]
  [b]else if[/b] [teal]([/teal]aDate [teal]<[/teal] bDate[teal])[/teal]
    [b]return[/b] [teal]-[/teal][purple]1[/purple]
  [b]else[/b]
    [b]return[/b] [purple]1[/purple]
[teal]}[/teal]
Note that your regular expression only took the century of the year.
And extracting the entire date in a single piece does not make it better sortable. You have to move the year in front of month and day, so captured it and reversed the groups.


Feherke.
feherke.github.io
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top