Stable sorting

If you are sorting data… the entries in the target column that needs be sorted are called keys. If two rows have the same key you can decide how the remaining column entries are treated. Stable sorting lets you preserve the original relative ordering of non-key columns.  With the UNIX sort command, you can use

 $ cat data
1 2
1 1
2 4
2 3

 


$ cat data | sort -nk1,1
1 1
1 2
2 3
2 4

 


$ cat data | sort -nk1,1 -s
1 2
1 1
2 4
2 3
Advertisements
This entry was posted in Scripting and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s