Forum > Windows
Universal Sort in Lazarus / Freepascal
ratmalwer:
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.
What did I miss??? Where is this documentation I did not find?
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function Compare(const ARecord1, ARecord2:TArrayA):Integer;// EQ = 0 , GR = 1 , LESS = -1 // Tipp: für DESC einfach das Result umkehren... ungetestet mk.// Strings ev mit UTF8Compare vergleichen// Upper / Lowercase durchdenken und implementieren begin Result := 0; // FELD 1 Result := UTF8CompareStr(Uppercase(ARecord1.String5),uppercase(Arecord2.string5)); 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 // FELD 3 begin if ARecord1.Field3 > Arecord2.Field3 then begin Result := 1; end else begin if ARecord1.Field3 < ARecord2.Field3 then begin Result := -1 end else // FELD 4 ..... begin // ...... usw.... end; end; end; end; end; end;end; procedure qsort_2(lower,upper :integer);var left, right :integer;pivot :TArrayA;temp : TArrayA;begin pivot:= ArrayA[(lower + upper) div 2]; //Pivot in mitte setzen left := lower; right := upper; while left<=right do begin while Compare(ArrayA[left],pivot) = -1 do left := left+1; // kleiner: left erhöhen wenn bereits geordnet while Compare(ArrayA[right],pivot) = 1 do right := right-1; // grösser: right vermindern wenn bereits geordnet if left<=right then // dies vergleicht den index aber nicht den Wert! begin //SWAP Record temp := ArrayA[left]; ArrayA[left] := ArrayA[right]; ArrayA[right] := temp; left:=left+1; right:=right-1 end; end; if right>lower then qsort_2(lower,right); { Sort the LEFT part } if upper>left then qsort_2(left ,upper); { Sort the RIGHT part }end;
And a extraquestion:
Is there a function-summary with brief examples like in php-manual somewhere? At the moment I program by try and error and I am a bit tired to check each found functions to detrermie what the returnvalues could be.
marcov:
Some points wrt collection sorting:
- Afaik TArray<> has a sort routine. At least in Delphi it does. Might be only in FPC trunk though. (packages/rtl-generics)
- TStringlist can be kept sorted, no need to do ad-hoc sorting.
- Compare routines should be configurable. E.g. to pick a natural compare over a normal one. Asc/desc is then just an inverted compare routine.
- I don't know PHP or its manual, and I like to keep it that way.
Mike.Cornflake:
You ask a good question (sorting for records)
Have a look at the following:
http://wiki.freepascal.org/Array_sort
As the header says, that was prior to generics. The idea in this wiki is that there is a general sort function, you just pass it a specific function that determines the rules for how one array element should be compared to another. (ie, based on two index, you return -1, 0 or 1). The actual soft algorthm is handled in the AnySort code. That idea gets used a lot.
Instead of an array, you could use generics, where you use a collection class to handle your data. Have a look at this post for instance
http://forum.lazarus.freepascal.org/index.php?topic=17772.0
http://wiki.freepascal.org/Generics
As for the help you are looking for, as a community there's been an effort to keep http://wiki.freepascal.org updated.
Welcome to the forum by the way :-)
BeniBela:
I made one, too 8-) https://github.com/benibela/bbutils/blob/master/bbutils.pas#L4541
Mike.Cornflake:
And time for a confession. I've got lazy in my old age. I generally just built up a TBufDataset and sort that. Cause I know I'm going to want to filter that data a few different ways as well.
TBufDataset is an in-memory data store. Think of a Table in a database.
TBufDataset is memory overkill for for most of my uses, but my code in all the different apps is a darned site more consistent, and I've created a helper unit to simplify creation, population etc.
And I've only just started using SQLite. Seriously thinking about switching over to that instead...
Navigation
[0] Message Index
[#] Next page