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-2017 by Barry MacDonnell. All Rights Reserved.

Page updated May 5, 2016

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

Sorting on the second column of a table

Using a table to sort regular paragraphs in reverse order

Related tips on other pages:

Sorting and converting existing merged address labels (by last name, by ZIP code, etc.)










[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, 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]).

  • 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 item 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.


This first example is the most common type of sort.

EXAMPLE 1: Sort on the first word on each item in a list.

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: Do a Line sort on first..ninth words on each item using a macro.


Here is a small macro that does an ascending Line sort with nine (9) sort keys (upper case sorts first for duplicates). [To copy this code into your WordPerfect program to create a working macro, see here.]

This method might be useful to sort lists where there are possible first (and second, etc.) word duplicates.

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.)

// 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!)
Sort
Return

EXAMPLE 5: Sorting a table
  • 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.
  • 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. You can define additional rules with the Add Key button.
  • Normally, lowercase sorts before uppercase. To reverse this, click the Options button and choose "Uppercase sorts before lowercase."
  • To sort by the last word in a particular column's cells, define the column number to use and then use "-1" (without quotes) in the Word field, "-2" for the next to last word, etc. 
  • Notes:
    • 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 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: Sorting on the second column of a table

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 6: 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:

1. Select all paragraphs in the document. 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.

2. 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.

3. Enter a 1 in the top cell of the first column, and a 2 in the cell just below it. Select just the entire first 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.

4. 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.

5. Place the cursor in the first (numbered) column. Click Table, Delete, Columns, 1, OK. You should now have a one-column table again.

6. 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.