Skip to content
Programmingoneonone
Programmingoneonone
  • Home
  • CS Subjects
    • IoT ? Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone

HackerRank Sort Command #4 problem solution

YASH PAL, 31 July 2024

In this HackerRank Sort Command #4 problem solution, You are given a file of text, where each line contains a number (which may be either an integer or have decimal places). There will be no extra characters other than the number or the newline at the end of each line. Sort the lines in descending order – – such that the first line holds the (numerically) largest number and the last line holds the (numerically) smallest number.

Input Format

A text file where each line contains a number as described above.

Output Format

The text file, with lines re-ordered in descending order (numerically).

The ‘Sort’ Command Line Program

This is frequently used for sorting input in text or TSV formats, in various different ways supported by it; which may be either lexicographical, case insensitive, based on the numeric field only, based on a particular column, etc.

Here are some quick notes about ‘sort’. The working examples provided in the added reference links might also help.

The vanilla ‘sort’ command simply sorts the lines of the input file in lexicographical order.

The -n option sorts the file on the basis of the numeric field available, if the first word or column in the file is a number.

The -r option reverses the sorting order to either the reverse of the usual lexicographical ordering, or descending order (while sorting in numerical mode).

The -k option is useful while sorting a table of data (tsv, csv etc.) based on a specified column (or columns).

The -t option is used while specifying a delimiter in a particular file (where columns are separated by tabs, spaces, pipes etc.).

HackerRank Sort Command #4 problem solution

Problem solution.

sort -gr $1

Second solution.

sort - -rn

Third solution.

sort -n -r 

Fourth solution.

cat > ntest
sort -rn ntest

coding problems solutions linux shell

Post navigation

Previous post
Next post

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes