Thanks guys for your replies...
I glanced trough it an will look at the code more closely later.
But actually I am a bit disapointed of the capability of Freepascal. In other languages there are predefined functions for such matters that are reliable, easy to use and well and brief documented. When I red that only integes can be sorted, or that records are not supported and only one sortcriteria is accepted - that makes me whine.
So I probably go on with my solution (so I know at least whats going on) witch cost me tree days now for for something I was used to make in 5 minutes by a command like: sort(array1,filed1,asc,field2,desc,field3,asc)
Hello
I was searching the net for an universal-sort for an array of records or some universal sort with tha ability to handle: miltiple fields, ASC /DESC, casesensitiity, and Unicode (UTF8)-support.
I did not find it so I started programming my own (code for a shellsort below - not finished but working fine).
Now I read in a old article: there are plenty or sortfunctions in Freepascal.
@BeniBela
your code look awful long.... Is it reliable?
If so could you send me an examle how to implement it. I am using Freepascal/Lazarus only since a month and struggle with implementing a lot.
But actually I am a bit disapointed of the capability of Freepascal. In other languages there are predefined functions for such matters that are reliable, easy to use and well and brief documented.
data structures should not matter. "What does something need to define as function to be sorted ?" that's all that matters. Things that can be sorted should implement these functions and it works.
Code: [Select]if Result = 0 then
begin
if ARecord1.String5 < ARecord2.string5 then
begin
Result := -1
end else
// FELD 2
begin
if ARecord1.Field2 > Arecord2.Field2 then
begin
Result := 1;
end else
begin
if ARecord1.Field2 < ARecord2.Field2 then
begin
Result := -1
end else
Result:= CompareStringField( Field1, AObj.Field1 );
if ( Result = 0 ) then
Result:= CompareStringField( Field2, AObj.Field2 );
if ( Result = 0 ) then
Result:= CompareStringField( Field3, AObj.Field3 );
if ( Result = 0 ) then
Result:= CompareIntField( Field4, AObj.Field4 );
end;[/code]
data structures should not matter. "What does something need to define as function to be sorted ?"
that's all that matters. Things that can be sorted should implement these functions and it works.
data structures should not matter. "What does something need to define as function to be sorted ?"
Sorted how? And how about multi key sorting, like the OP hints on?Quotethat's all that matters. Things that can be sorted should implement these functions and it works.
Oversimplification.
data structures should not matter. "What does something need to define as function to be sorted ?"
Sorted how? And how about multi key sorting, like the OP hints on?Quotethat's all that matters. Things that can be sorted should implement these functions and it works.
Oversimplification.
Well, I don't want to reply in the name of the quoted user.
But...
Actually with something like JavaScript array sort or Java Comparable / Comparator the method decides how to sort.
or with https://www.freepascal.org/docs-html/rtl/classes/tlist.sort.html
It is just that you need to define a method/comparator. Tlist doesn't require a swap because the sizes are uniform (all pointers)