LiveCode LessonsLiveCode Data Grid Working With Data Grid FormsHow Do I Sort Records By A Specific Key's Values?

How Do I Sort Records By A Specific Key's Values?

You can sort the rows of a data grid form using the SortDataByKey command. Let's look at an example.

Example

Example

This card has a data grid and an option menu. The option menu contains three values that you can sort by: First Name, Last Name and Title.

Option Menu Code

Option Menu Code

The code to perform the sort is pretty straight forward.

1) Determine which array key of the data grid form to sort by. This will be one the keys you created when you assigned the dgData. If you used the dgText property then the key will be "Label 1" or "Label 2", etc.

2) Determine the sort type, direction and whether or not the sort is case sensitive.

3) Call the SortDataByKey command.

The Result

The Result

Here is what the sort looks like after selecting First Name.

2 Comments

François

I've tried to sort the records by a date field. Of course my dates are formatted dd/mm/yyyy (European style). The sort doesn't seem to work right. The records are not sorted according to the English format (the years are not sorted right).
I did:
on mouseUp
set the useSystemDate to true
dispatch "SortDataByKey" to group "DataGrid1" with "theDate","dateTime","ascending","false"
dispatch "RefreshList" to group "DataGrid1"
end mouseUp
Any idea what can be wrong?

Trevor DeVore

@Franços - useSystemDate is a local property meaning it won't affect the code that is running in the SortDataByKey handler you send to the data grid. Rather than passing in "dateTime" try passing in "system date time".

Add your comment

E-Mail me when someone replies to this comment