2

I got an array of strings with format: "XXXX YYYY ZZZZ WWWW"

i need to sort by YYYY; but if i use the common "sort", will be sorted by XXXXX not by YYYY.

I tried to put each element into a diccionary, using the YYYY as key; and using the key of diccionaries as array, then sort this array and take the elements. Anyone thinks another format, "more elegant"?

2 Answers 2

2

You can definitely still use the sort command.

From the Linux manual page for sort:

NAME sort - sort lines of text files

SYNOPSIS sort [OPTION]... [FILE]... sort [OPTION]... --files0-from=F

DESCRIPTION Write sorted concatenation of all FILE(s) to standard output.

Mandatory arguments to long options are mandatory for short options too.

-k, --key=KEYDEF sort via a key; KEYDEF gives location and type

And so you can do this:

sort -k2

And it will sort your input based on the 2nd key assuming the default delimeter is whitespace.

Sign up to request clarification or add additional context in comments.

1 Comment

Reading the manual i skip this parameter. Sorry for silly question
1

Use the sort command with -k 2 in order to act on second field.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.