Order Range / setrange by multiple columns

Is there a possibility to get a range that is sorted by two columns?

Example:

Tab1

Sort by Column1 first and then by Column2

Tab1

Column1 Column2

1 Date 1

2 Date 3

2 Date 5

3 Date 2

3 Date 4

3 Date 6

4 Date 7

procedure SortedData ()
var
Tab1: Record “Tab1”;
begin
Tab1.SetRange(“Column1”, “Column1”);

//…What do i have to do now to sort by column 2 as well?

end;
end;

Hi,

Using SETRANGE doesn’t change how your data is sorted! It only limits which rows are displayed. It’s “just” a filter.

If you like to change the way data are sorted, then you have define a new key including the fields it should sort by.

In your example, then I assume that you only have two columns/fields in the Tab1 table. In that case Column1 would be the primary key (if the same date is only allowed once). So technically it would always sort by first the primary key and then the first (and only) field. If the same date is allowed more than once, then you need more fields in your primary key, otherwise the key would not be unique.

If column1 and column2 are only two fields in the table, then you would need define a new key with Column1 and Column2.

Column 1 and column 2 are only two fields in the table (no primary key).

I used SetRange, SetFilter, SetCurrentkey and FindLast to get the information i wanted. Example:

procedure SortedData ()

var
Tab1: Record “Tab1”;
begin
Tab1.SetRange(“Column1”, “Column1”);

Tab1.SetFilter(“Column1”, “Column1”);
Tab1.SetCurrentKey(“Column2”);
if Tab1.FindLast() then begin

end;
end;

Another way that i learned about just now is to use SECURRENTKEY for two fields:

SetCurrentKey(“Column1”, “Column2”)

What do you think [mention:61b2aa9ce72e429baa1ef43208ddbea4:e9ed411860ed4f2ba0265705b8793d05] ?

other post about this topic: https://dynamicsuser.net/nav/f/developers/7532/multiple-keys-and-how-they-are-used