Page 1 of 1

Sorting with more than 3 keys

Posted: Fri May 08, 2009 10:51 am
by Montaigne
Is this possible?

I have a flat file that I need to sort using 6 keys.
I can't find another question along these lines, but I can't believe that no-one else has needed to sort with more than 3 keys before.

Could I get help...or a pointer to a forum where this has already been discussed?

Or is this as good as it gets?

Thank you.

Montaigne

Posted: Fri May 08, 2009 11:44 am
by ACRobin
The Help says sorting of 3 keys.

My suggestion would be to move the columns you want to sort next to each other and then select the larger key - this does need a bit of planning and you may not get the results you want - depends on the data.

Posted: Thu May 28, 2009 4:36 am
by sosimple
I can think 3 ways to do this. Some are a little complicated but I guess it depends on personal preference.

First, you could do as suggested by ACRobin and switch to block mode and move all the columns of data that you are sorting on together (say at the beginning of the line) and sort as a one continuous field.

When you are done sorting, you then need to move all the columns back where they belong.

---

A second way (my preference) is to COPY the columns of data that you are sorting on to the beginning of the line and sort as a one continuous field.

Then, when you are done sorting, you can just delete the copied columns of data from the beginning of the line and what remains is the original data, sorted as desired. No need to re-position columns because you didn't move any data.

If you are sorting all of your keys the same, either all ascending or all descending, then either of these two methods will work fine.

---

The third way does not involve moving or copying your data.

First sort the file as desired based ONLY on the LAST THREE search keys (key 4/5/6).

Then, number the lines by using a RegEx Search/replace:
Find:^.
Replace with:\i(10001)-&
[x] Regular expression

(x) Active document

Replace All

Now, sort using key 2 and key 3, and use the leading number that was inserted, as your third key.

Next, re-number the lines by using RegEx Search/replace:
Find:^[0-9]+-
Replace with:\i(10001)-
[x] Regular expression

(x) Active document

Replace All
Now, sort using key 1, and use the leading number that was inserted as your key 2.

Then, delete the leading number that was inserted (and the "-"), and what remains is the original data, sorted as desired.

The advantage of this method is that each key can be sorted ascending or descending, as required.