Foxpert Software Development & Consulting

Menu

Whitepapers
Downloads
Knowlbits
Guineu

2006-09Sep-25

Sorting

You find sorting algorithm in most programming languages, but you have to look hard for an implementation of common sorting algorithms in Visual FoxPro. Of course, Calvin Hsia has some on his blog, but other than that it's getting difficult. The most obvious reason is that Visual FoxPro supports sorting natively for indexes as well as arrays.

Sorting mixed strings

If you have data like
1/1A
1/1B
2/A
10/2
10/3

you want to sort the first part as if it's numerical. That is, the order should be 1, 2, 10, not 1, 10, 2 as it would happen, if Visual FoxPro treats this as a string. At the same time, however, you want to sort the remaining part in alphabetical order. The following index expression accomplishes that:

INDEX ON STR(VAL(field)) + field TAG _Sort

Sorting with COLLATE

Virtually every application picks only one COLLATION sequence it uses all the time. The majority uses MACHINE, followed by GENERAL, followed by country specific versions. However, sometimes you need to sort and search data with a different order. For example, you might use MACHINE as the collate sequence, but in some cases need to sort case-independent (that's just an example!).

When you create an index, Visual FoxPro uses the active COLLATE sequence or the one you specified with the COLLATE clause. A little known fact is that Visual FoxPro always uses the COLLATE sequence that belong to an index when Visual FoxPro searches or sorts using the index. This is true even when the application switched to a different COLLATE sequence:

Create Cursor curTest (cName C(10))
Insert into curTest Values("Straße")
Set Collate To "GERMAN"
Index on cName Tag cName
Set Collate To "MACHINE"
? Seek("Strasse")  && Prints .T.

Previous KnowlBits

RSS

February 2011 (2)

December 2010 (1)

October 2009 (2)

September 2009 (1)

August 2009 (4)

July 2009 (2)

June 2009 (2)

May 2009 (1)

April 2009 (1)

March 2009 (1)

August 2008 (1)

July 2008 (2)

May 2008 (1)

April 2008 (2)

January 2008 (2)

December 2007 (2)

November 2007 (2)

October 2007 (1)

September 2007 (1)

August 2007 (5)

July 2007 (4)

May 2007 (6)

March 2007 (3)

February 2007 (7)

January 2007 (6)

November 2006 (1)

October 2006 (3)

September 2006 (10)

June 2006 (2)

May 2006 (6)

April 2006 (1)


Impressum Kontakt Contact