Home | Tips | Library | Other Authors | Other WP Sites | Writer's Links | Contact | Site Map | Donate

Barry MacDonnell's
Toolbox for WordPerfect

Macros, tips, and templates for Corel® WordPerfect® for Windows®
© Copyright 1996-2018 by Barry MacDonnell. All Rights Reserved.

Page updated Mar 26, 2018

WordPerfect Tips
Main tips page | Browse tips
How to use the Sort feature - some tips and examples

Before you begin

Examples:

Sort on the first word on each item in a list

Sort on the second word on each item in a list (such as a list of titles that all begin with "The")

Sort on the last (significant) word on each item in a list such as the last name on FirstName_Lastname lists.

Do a Line sort on first..ninth words on each item using a macro.

Sorting a table

Table as a tool: Sorting on the second column of a table

Table as a tool: Using a table to sort regular paragraphs in reverse order

Related tips on other pages:

Table as a tool: Sorting and converting existing merged address labels (by last name, by ZIP code, etc.)

•  Table tips: Using WordPerfect Tables










[Note: The first group of these methods refers to typical (manual) methods of sorting. Scroll down for methods that use macros or table columns to sort.]

WordPerfect's Sort feature can be useful to sort lines, paragraphs, labels -- or even parallel column rows, table rows, or merge data records.

You can access Tools, Sort on any <WordPerfect> menu. (If you don't see it, you might have enabled a <Microsoft Word> menu; try choosing a <WordPerfect> menu on the Tools, Settings, Customize, Menus tab.)

Don't forget to review WordPerfect's own help file (F1 key: search for "sorting").

See the examples below and the links in the left sidebar.

BEFORE you begin here are some tips and cautions:

•  Always make a backup of your document before sorting, just in case.

•  Single lines vs. paragraphs.

- A list of just names, titles, etc., should have a single hard return ([HRt]) at the end of each line. Thus you can sort them by line.

- Similarly, blocks of wrapped text (i.e., several lines ending with soft returns [SRt]) where the last line ends with a single hard return can be sorted by line. If they end with two or more hard returns (or a hard page break [HPg]) they will be sorted by paragraph.

- If the list is in multi-line format (e.g., name/address labels, where some lines in the items should not be included in the sort) then each paragraph should be separated with two hard returns (or a hard page break [HPg]).

•  Codes in brackets (e.g., [HRt], [SRt], etc.) below are text representations of WordPerfect codes visible in the Reveal Codes window.

• 
If the items are not in a separate document with no other text, be sure to select the items before sorting them.

Selecting the text tells WordPerfect exactly which items you wish to sort.

•  Indented, flush right lines, or other "leading" codes.

If you have indented, flushed right, or adjusted lines or paragraphs in your list, be aware the format codes responsible for these adjustments to text can cause WordPerfect to count them as fields in a sort, so the field number specified in the sort keys in the examples below might be incorrect. [See also the next bulleted items about codes.]

Solution:
Use Reveal Codes to determine how many such "fields" are at the beginning of each line in your list, and adjust the number in the "Field" mentioned in the instructions below.


•  Lines that begin with quote marks. Need to sort words or paragraphs that begin with quote marks but should be sorted by the first text character, not the quote mark? Here's a tip by Noal Mellot on WordPerfect Universe:

"...Unfortunately, [an] alphabetical sort is not purely alphabetical, since punctuation marks and symbols at the start of a word introduce a degree of disorder. Let's say quote marks are keeping you from obtaining the sorting results you expect when sorting on the first word in a field. A macro could search for the initial quote mark, delete it, search for the final quote mark, delete it, space, then type a symbol (such as a spade) as a signal that the preceding word should be surrounded by quote marks. You could then sort normally on the first word. Once the sorting is done, another macro could search for the symbol, delete it with its space, back up to the end of the preceding word, type a quote mark, then move to the start of the word and type a quote mark."

•  Other format codes such as [Bold] or [Italc] or [Und] that are applied to the beginning of the text string on any given line can cause WordPerfect to place these codes at inappropriate locations during a sort, causing unwanted changes in such formatting in the final sorted list.

Solution: Place an "empty" item at the top of the list (i.e., a single hard return) and include it when you sort the list of items. [N.B.: This appears to be a small bug that has existed since at least WordPerfect 8, up through WPX5/service pack 2 (at the time of this post).]

•  Hidden text.

Text that might otherwise be used as a sort key item (e.g., the first word in a line or paragraph) will not be "seen" by the sort feature if you mark it hidden. This might be a useful way to temporarily remove the word(s), punctuation, etc., from being considered by the sort feature. Just apply the Hidden attribute (e.g., with Format, Font, Hidden) to the items; hide the items by un-checking the View, Hidden Text option; perform the sort; then show the hidden text by enabling that View menu option.

•  Length of items.

Tests by several WPU members indicate that sorting by Line works on numerical words up to and including the 14th digit, and on alphabetical words up to and including the 19th character. (See thread here. Note that earlier versions of WordPerfect may be even more limited.)

•  Size of list.

According to some reports, the WordPerfect program (at least up to WPX4 (build 14.0.0.756)) may limit the number of items it can sort to 32K of memory. This seems true of paragraph sorting, and may be true of line sorting.
EXAMPLES

EXAMPLE 1: Sort on the first word on each item in a list (the most common type of sort).

1. Click on Tools, Sort. This opens the Sort dialog.

2. Choose one of the standard sort keys, such as "First word in a line" (if each item ends with a single hard return), or "First word in a paragraph" (if each item ends with two hard returns or a hard page break).

Note

If you want to also sort on the second word, third word, etc., in case you have duplicate first (and possibly second) words, you'll need to create a custom Key 2, Key 3, etc., with a different number in the "Word" fields. See the examples below that show how to add Keys. Note also that each part of an Internet address (URL) that is separated by a slash is a "word" to the sort function, so additional custom keys would be useful to sort them correctly.

3.
You may want to click the Options button to "Allow Undo after sorting" and/or "Uppercase sorts before lower case". Most users will accept all other defaults in that dialog, but feel free to modify them.

4. Click Sort.


The next two examples show how to create a new custom "sort key" if you don't already have one for your particular sort needs. You can also use the Edit button in the Sort dialog to modify an existing sort key.

EXAMPLE 2: Sort on the second word on each item in a list (such as a list of titles that all begin with "The")
.

1. Click on Tools, Sort. This opens the Sort dialog.

2. Click on the New button to open the New Sort dialog.

3. In the "Sort Description" field, enter a name (e.g., "Sort by second word").

4. Click the Sort by Line radio button. [Note: If the list is in multi-line format -- e.g., name/address labels -- with each item in the list separated with two hard returns (or a hard page break [HPg]), then use the Sort by Paragraph radio button.]

5. Under "Keys (sorting rules)":

•  For Line sorts: For Key 1, "Field": enter "1" (without quotes); for "Word": enter "2" (without quotes).

•  For Paragraph sorts: For Key 1, "Line": enter "1" (without quotes); "Field": enter "1" (without quotes); for "Word": enter "2" (without quotes).

•  For "Sort order" you should choose Ascending if you want the sort to be a..z.

6. Click OK to dismiss the New Sort dialog and return to the Sort dialog.

7. You may want to click the Options button to "Allow Undo after sorting" and/or "Uppercase sorts before lower case". Most users will accept all other defaults in that dialog, but feel free to modify them.

8. Click Sort.


EXAMPLE 3: Sort on the last (significant) word on each item in a list such as the last name on FirstName_Lastname lists.

Tips

€  Before sorting, attach any suffixes such as Jr., III, PhD., etc., to the last name with a hard space (Ctrl+spacebar) so that WordPerfect sees the surname and the suffix as a single word. This can be done manually by replacing each such space with a hard space, or a bit more easily with Find and Replace, using the Replace..Codes button to insert a [HSpace] code in the Replace with field. Hence, for example: Search for <space>Esq. and replace with [HSpace]Esq.

€  If the words (e.g., surnames, nine-digit ZIP codes, etc.) you wish to sort on might be hyphenated with "hard" hyphens (i.e., the "-" hyphen character, not the regular [- Hyphen] code), you should first convert them to regular hyphen codes. Otherwise, WordPerfect will see the hyphenated word as two words. To convert them: Go to the top of the document, use Edit, Find and Replace, and type a Ctrl+hyphen in the Find field and type just a hyphen ([- Hyphen] code) in the Replace field. Then use Find Next to examine each found item (in Reveal Codes) to see if it should be replaced.

€  If you wish addresses to sort on a ZIP code, the addresses should all be the same basic paragraph format, such as all paragraphs having three (or whatever number of) lines. Then you can use the Paragraph sort below, and use the correct line number (e.g., 3) in the Line field that the ZIP code is on in your labels. (See also the tip above on hyphens.)

1. Click on Tools, Sort. This opens the Sort dialog.

2.
Click on the New button to open the New Sort dialog.

3.
In the "Sort Description" field, enter a name (e.g., "Sort by last word").

4.
Click the Sort by Line radio button. [Note: If the list is in multi-line format -- e.g., name/address labels -- with each item in the list separated with two hard returns (or a hard page break [HPg]), then use the Sort by Paragraph radio button.]

5.
Under "Keys (sorting rules)":

•  For Line sorts: For Key 1, "Field": enter "1" (without quotes); for "Word": enter "-1" (without quotes).

•  For Paragraph sorts: For Key 1, "Line": enter "1" (without quotes); "Field": enter "1" (without quotes); for "Word": enter "-1" (without quotes).

•  For "Sort order" you should choose Ascending if you want the sort to be a..z.

•  If you are sorting on ZIP codes, see the Tip above.

•  If you are sorting people's names and you also want to sort duplicate surnames alphabetically by the person's first name, add another key (Key 2) with a "1" (without quotes) in the Line, Field, and Word fields (assuming the first name is the first word on each line).

6.
Click OK to dismiss the New Sort dialog and return to the Sort dialog.

7.
You may want to click the Options button to "Allow Undo after sorting" and/or "Uppercase sorts before lower case". Most users will accept all other defaults in that dialog, but feel free to modify them.

8.
Click Sort.


OTHER WAYS TO SORT

EXAMPLE 4: Macro example to do a Line sort on first..ninth words on each item


Here is a small macro that does an ascending Line sort with nine (9) sort keys (upper case sorts first, but you could change to LowercaseFirst! in the macro's SortCaseOrder command).

This method might be useful to sort lists where there are possible first (and second, etc.) word duplicates. It should work on a selection of lines, too.

Tip: You could, of course, manually create a custom sort such as those above, by adding additional Keys with incrementing "Word" values for each Key.

Note: Using this macro routine I recently discovered that sorting very long lists (or possibly long lists with some long items) can cause the routine to fail and possibly cause WordPerfect to freeze. My solution for that document was to use just one sort key, not nine keys. (I did not test other variations, such as eight keys, seven keys, etc., nor did I try varying the length of the list to be sorted. Hence, this notation is merely an an anecdotal observation and may not affect you.)

[To copy this code into your WordPerfect program to create a working macro, see here.]

// Sort on up to 9 words per line ("Line sort"):
SortKeys (
{1; 1; 1; Alphanumeric!; Ascending!};
{1; 1; 2; Alphanumeric!; Ascending!};
{1; 1; 3; Alphanumeric!; Ascending!};
{1; 1; 4; Alphanumeric!; Ascending!};
{1; 1; 5; Alphanumeric!; Ascending!};
{1; 1; 6; Alphanumeric!; Ascending!};
{1; 1; 7; Alphanumeric!; Ascending!};
{1; 1; 8; Alphanumeric!; Ascending!};
{1; 1; 9; Alphanumeric!; Ascending!})
SortType (LineSort!)
SortUndo (Undo!)
SortCaseOrder (UppercaseFirst!)
SortSelectString ("") // (i.e., null, or empty)
Sort
Return
// End of macro

Note that this macro does not create a new key in the Sort dialog. It's a one-off sort performed by the macro each time you play it.

EXAMPLE 5: Sorting a table

[See also the examples on using a table as a tool below.]

•  It is a good idea to save the document before performing any sort on it. You can also enable "Allow Undo" in the Tools, Sort, Options button drop list.

•  The individual items in a sort are considered to be "records." WordPerfect lets you sort five kinds of records:

▸ lines (each ends with one hard return),
paragraphs (each ends with multiple hard returns),
merge records,
parallel columns (table column or parallel column), and
the rows in a table.

•  Each row of a table is a record, and it is divided into its parts:

Column (cell), Line (text in a cell), and Word (text in a cell).

Cells are numbered from left to right, starting with cell 1.


•  WordPerfect ships with several types of sort, as shown in the "Sort by" field. Select the one that applies to your text.

Note the Options button might have value to your current sort: "Allow Undo after sorting" and Uppercase sorts before lower case (default: Lower case sorts before upper case).

•  To define a custom table sort:

Place your cursor in the table, click Tools, Sort, New.

Give the sort a name (e.g., "My New Sort").

Then with the Table Row radio button enabled, define the "Sort by" rule (or key), which defines the order in which the sort will be conducted.


Tip:

You can define additional rules with the Add Key button.


For example, if the first key sorts on the First cell in a table row, it will sort on the first word in that cell -- i.e., Column 1 (i.e., first cell), Line 1 (current row), Word 1 (first word in the cell). You can add another key to also sort on the second word if the first word is the same as the first word in other cells in that column (e.g., "Accounting"). Similarly you can add a third key to sort on the third word. (Thus, "Accounting Department, Payables" would come before "Accounting Department, Receivables".)

WordPerfect allows using up to nine sort rules or "keys" to be used in a sort. Just add more keys (up to 9 maximum) with the same specification (i.e., Column 1, Line 1) but with a new Word number fro each key (2-9).

Note that very large tables or tables with large amounts of text in them might cause the sort to fail when using so many keys, possibly causing WordPerfect to freeze. (This is merely based on just a few anecdotal reports. Hence, always make a backup of the document first.)

Here's a macro snippet (similar to the line sort in Example 4 above) that uses all nine keys to sort a table (or a selection of table rows) alphanumerically in ascending order, lower case first, and allowing you to Undo the result with Ctrl+Z.

[To copy this code into your WordPerfect program to create a working macro, see here.]

// Sort on up to 9 words in the first cell of each row ("Table sort"):
SortKeys (
{1; 1; 1; Alphanumeric!; Ascending!};
{1; 1; 2; Alphanumeric!; Ascending!};
{1; 1; 3; Alphanumeric!; Ascending!};
{1; 1; 4; Alphanumeric!; Ascending!};
{1; 1; 5; Alphanumeric!; Ascending!};
{1; 1; 6; Alphanumeric!; Ascending!};
{1; 1; 7; Alphanumeric!; Ascending!};
{1; 1; 8; Alphanumeric!; Ascending!};
{1; 1; 9; Alphanumeric!; Ascending!})
SortType (TableSort!)
SortAction (Sort!)
SortUndo (Undo!)
SortCaseOrder (LowercaseFirst!)
SortSelectString ("") // (i.e., null, or empty)
Sort
Return
// End of macro

Note that this macro does not create a new key in the Sort dialog. It's a one-off sort operation performed by the macro each time you play it.

•  As mentioned, normally lowercase sorts before uppercase. To reverse this, click the Options button and choose "Uppercase sorts before lowercase." (In the above macro change the command to SortCaseOrder (UppercaseFirst!).)

•  To sort by the last word in a particular column's cells, specify the column number to use and then use "-1" (without quotes) in the Word field, "-2" for the next to last word, etc.

•  The same basic snippet could be used to (e.g.) sort ordinary lines of text NOT inside a table, such as in a list of terms with each on its own line: Just change the SortType(...) parameter from TableSort! to LineSort!.

Notes and tips:

€  WordPerfect format codes ([Tab], [Hd Left Ind], etc.) are treated as field separators by the sort feature. Hence it might be a good idea to look over the table in the document and see if all rows used in the sort have the same "format" (i.e., same number and location of Tabs, etc.) if you wish to use a particular field to sort on.

€  On the other hand, spaces, forward slashes (/), and hyphens separate words.

€  Dates can be used as a Key Type (WordPerfect 10+), and each date in the table should be separated by slashes or hyphens; all characters are considered when sorting dates in numerical order. Hence, use the same format for all dates (e.g., 10 characters: 09/25/2013). Also: As noted, the slash or hyphen can be used to separate words, so parts of dates (e.g., the year) can be used as "words" to define a sort key for the dates you want to sort. See WordPerfect's Help <F1> or the Reference Center (on the Help menu) for more information.
EXAMPLE 6: Table as a tool: Sorting on the second column of a table

[Note that this example uses the second column to sort the table ... but any column can be used to sort the table. See below for more examples on using a table as a tool.]

Here's how to convert a "line item" list into a table to sort it by a specific column (e.g., the second column), then restoring the data to a list format.

Assuming there are just single spaces between the words in every item in the list (if not, you can use the DeSpacer macro to remove extra spaces)

and -

assuming the list is composed of items that end in single hard returns (i.e., the list is not a series of paragraphs)

then -

1. Select the entire list and then click on Table, Create. Enable the User Defined radio button in that dialog, and enter a single space in the small field to the right of that choice. (Press the spacebar once in that field.) Click OK.

This turns the list into a table with each word in a separate cell. Examine the first two columns; if any of them are empty cells, then the sorting results will not be correct. (You can Undo things with Edit, Undo or Ctrl+Z and make corrections to the list.)

2. Next, create a new sort Key (or edit an existing key), such as "Second cell in a table row," and change the "Column" field to "2" (without quotes). [If you need help, see the regular sort feature methods above about creating a custom sort Key.]

3. Sort the table. Put your cursor in the table and use Tools, Sort, and choose the sort Key (e.g., "Second cell in a table row.") (You can click the Options button to "Allow Undo after sorting".) Then click Sort.

The list, in Table form, should now be sorted on items in the second column (the "second word in the list").

5. Convert the table back into your original list format this way:

(a) Delete the [Tbl Def] code in Reveal Codes: Put the cursor to the left of that code and press the <Delete> key. The Delete Table dialog should appear.

(b) In the Delete Table dialog, choose "Convert table contents." Click OK, then choose "Convert tables to text...Separate text with other." Enter a single space in the small field to the right of that choice. (Click in that field and press the spacebar once.)

(c) Click OK.

Done! [N.B.: Tested in WPX5 but the process should be the same or similar in other versions.]


EXAMPLE 7: Table as a tool: Using a table to sort regular paragraphs in reverse order

If you are keeping a diary, journal, or other document where you enter new lines (or paragraphs) at the end of the document, but now want to reverse the order of the items so that the most recent are at the top, here's a good way to do it.

Note: Using a table also helps prevent changes to existing formatting in the paragraphs (such as existing automatic numbering scheme applied to the paragraphs). The table cells act like little protective containers for each of your paragraphs during the sort.

Tips: If the paragraphs are separated by more than one hard return code (i.e., displayed as [HRt] in Reveal Codes) and you want to remove the extra ones first ... this can be done with Edit, Find and Replace to replace the multiple adjacent codes with a single code. (For more on this see the Find and Replace tips here.) Removing extra hard returns is not required for the procedure below, and in fact might not always be desirable. You might actually want the extra space (which to the program in this case is an extra -- if empty -- paragraph). Work on a copy of your document and try it both ways, with the extra returns and without them.

1. Select all paragraphs in the document with your mouse or keyboard, or with Edit, Select, All (or Ctrl+A on the Windows keyboard).

2. Click Table, Create and set the Columns = 1 and Text Delimiters = Paragraphs. Click OK. You should now have a table with all document paragraphs in separate cells.

3. With the cursor in the table, click Table, Insert, Columns = 1, Before, OK. You should now have a two-column table with the first column being empty. (At this point don't worry about column widths since you will be removing this column later.)

4. Enter a 1 in the top cell of the first column, and a 2 in the cell just below it in the same column. Select just the entire first column (either "paint" all cells in that column, or place the cursor just under the top edge of the first cell in the column; when it changes to an up arrow you can double click there to select the entire column).

Then click Table, QwickFill. This should sequentially number all cells in the first column. Click in the second column (or outside the table) to deselect the first column.

5. Sort the table: With the cursor in the table, click Tools, Sort to bring up the Sort dialog.

Choose "First cell in a table row," then click New. Give the new sort routine a name (e.g., "Reverse table sort"). The "Sort by" radio buttons should be set to "Table row."

Then, under "Keys," set the Type = Numeric and Sort Order = Descending.

Click OK.

With the new routine name ("Reverse table sort") selected in the Sort dialog, click Sort. The entire table should immediately sort into reverse order.

6. Place the cursor in the first (numbered) column. Click Table, Delete, Columns, 1, OK. You should now have a one-column table again. (The width of the column can be ignored here.)

7. Final steps: With the cursor in the table, click Table, Convert. Select the "Convert tables to text" button, and the "Separate text with paragraphs (hard returns)." Click OK.

EXAMPLE 8: Table as a tool: Using a table to randomize a list of items - or even a block of sentences or paragraphs

See the Using WordPerfect tables page here.