Windows Command Line sort Tutorial

Windows Command Line sort Primer Tutorial

Windows Command Line sort Tutorial

If you were to nominate the most frequent “chore” you set your “software programs” to do, and we should never forget we are here to make software that achieves something, there is a big chance you’d choose the job of sorting data. The why I guess relates to how a lot of we humans want data presented … we seek order … and are generally a bit afraid of chaos … whether this is good always is for 56784533 other conversations to have.

Let’s go back to a thinking close to the operating system of interest … and today that is Windows (remember when we covered Linux sort Tutorial as shown below?) … specifically its command line, and only its command line … in the old days called DOS or (MS-DOS) … we set ourselves the task not to compile anything … just straight DOS batch … and look at some command line Windows Command Line that will work on most/all Windows operating systems … it is pretty fundamental stuff, and we use the same CSV input data file as for the tutorial below so that you can compare and contrast.

The base command of interest may amaze you … are you ready …


sort [inputFile]

… gasp … but sort just like that (without switches) assumes …

  1. alphabetical sorting (okay as switch in Linux sort but not in DOS sort, as a switch)
  2. total record sorting (in DOS sort can have a character position to start at as a switch, but this seldom suits CSV sorting) (as if all the characters on a carriage return/line feed delimited string)
  3. ascending sort (from least to greatest) (in DOS there is a “/r” switch (for reverse sort) available)
  4. the sort determination looks from the leftmost character and proceeds to the right

… but, today, with our piped sort commands we present switches that can work with the first three defaults, respectively, the first two of which we have to tailor some DOS Batch programming code …

  • /n (we invent for our job)
  • we will assume a comma as a delimiter and allow user to specify a (single) field number to sort on
  • /r (as said above, exists in DOS sort command now, and we will allow its usage … doh!)

… in other words …

  • numerical sort
  • sort by field [colNumber] where fields are separated by comma
  • descending sort

So doing this proved pretty ugly when you don’t involve any compiled code … and we came up with …

Link to the supervising csvsort.bat with the usage instructions …

@rem csvsort.bat
@rem RJM Programming – January, 2015
@rem Defaults to three comma separated fields to sort and default csv3.bat and vsc3.bat as written
@rem Usage: csvsort[.bat] [[numFields] [rowsToSkip]] inputCSVFilename [fieldNumberOfSort] [/n] [/r]
@rem Defaults: /n does numerical sort and /r reverses the sort
@rem Defaults: rowsToSkip=0 fieldNumberOfSort=1 (starting at 1) numFields=3 (if specified please also specify a rowsToSkip value)

Link to the supervised (and only suitable for 3 column record CSV files … if feeling strong, write others for your own purpose) … csv3.bat and vsc3.bat and the more generic lpad10.bat


Previous relevant and contrasting Linux sort Tutorial is shown below.

Linux sort Primer Tutorial

Linux sort Tutorial

If you were to nominate the most frequent “chore” you set your “software programs” to do, and we should never forget we are here to make software that achieves something, there is a big chance you’d choose the job of sorting data. The why I guess relates to how a lot of we humans want data presented … we seek order … and are generally a bit afraid of chaos … whether this is good always is for 56784532 other conversations to have.

Let’s go back to a thinking close to the operating system of interest … and today that is Linux … and look at some command line Linux that will work on most/all Linux or Unix operating systems … it is pretty fundamental stuff.

The base command of interest may amaze you … are you ready …


sort [inputFile]

… gasp … but sort just like that assumes some of the defaults (depending on your deep configuration settings (ie. in your profile, with an alias, you can change them)) …

  • alphabetical sorting
  • total record sorting (as if all the characters on a carriage return/line feed delimited string)
  • ascending sort (from least to greatest)
  • the sort determination looks from the leftmost character and proceeds to the right

… but, today, with our piped sort commands we present switches that can change the first three defaults, respectively …

  • -n
  • -k [colNumber] -t[delimiterCharacter]
  • -r

… in other words …

  • numerical sort
  • sort by field [colNumber] where fields are separated by [delimiterCharacter]
  • descending sort

We also touch on other commands with our look at Linux sorting of our input CSV data that we talked about with PHP/Javascript/HTML Google Chart JSON Data Table Import of CSV Tutorial as shown below that has fields “Country”,”Population”,”Crude Birth Rate” …

  • cat [CSVfileName] … type a file called [CSVfileName]
  • uniq … pipe to enforce record uniqueness
  • sort -u … pipe and switch to enforce field (of record) uniqueness
  • grep ‘[searchCriteria]’ … piped filter to include only records satisfying [searchCriteria]
  • sed ‘/$/s//,5/g’ … piped record modifier adding a new column (ie. ,5) to right of our input CSV data

Hope you find some interest in this Linux operating system command discussion. This is raw command line talk but it can also be supervised as a useful component tool by, (amongst other ideas by) …

  • web server side language supervision … eg. via PHP exec for example, or via curl (of that PHP)
  • called by compiled desktop program, such as Java, C++, C
  • called by a Linux/Unix shell script … in Bourne Shell, Korn Shell, C Shell, tcsh, Unix Shell

Previous relevant data from PHP/Javascript/HTML Google Chart JSON Data Table Import of CSV Tutorial is shown below.

PHP/Javascript/HTML Google Chart JSON Data Table Import of CSV Tutorial

PHP/Javascript/HTML Google Chart JSON Data Table Import of CSV Tutorial

Here is a tutorial that introduces you to Google Graphs API, or Google Chart Tools, and its JSON Data Table functionality.

Google Chart Tools provide a perfect way to visualize data on your website. From simple line charts to complex hierarchical tree maps, the chart galley provides a large number of well-designed chart types. Populating your data is easy using the provided client- and server-side tools.

Thanks to the World Bank for some statistics which helped a lot.

Let’s see some PHP code in live action for this tutorial where you see a JSON Data Table derived from a filename passed as a parameter in the URL. The data shown is based on a CSV file of a previous PHP/Javascript/HTML Google Chart Intensity Map Tutorial shown below.

Link to Google Chart Tools “spiritual home” … via Google.
Link to Google Chart Tools JSON Data Table information … via Google.

Link to some downloadable PHP programming code … rename to chart_editor_in.php.
Link to some downloadable PHP programming code … rename to json_data_table.php.
Link to some downloadable input CSV data … rename to Google_Chart_Intensity_Chart_Tutorial_as.CSV.


Previous PHP/Javascript/HTML Google Chart Intensity Map Tutorial below …

PHP/Javascript/HTML Google Chart Intensity Map Tutorial

PHP/Javascript/HTML Google Chart Intensity Map Tutorial

Here is a tutorial that introduces you to Google Graphs API, or Google Chart Tools, and its Intensity Map functionality.

Google Chart Tools provide a perfect way to visualize data on your website. From simple line charts to complex hierarchical tree maps, the chart galley provides a large number of well-designed chart types. Populating your data is easy using the provided client- and server-side tools.

Thanks to the World Bank for some statistics which helped a lot.

Let’s see some PHP code in live action for this tutorial where you define your intensity map characteristics and data.

Now part of an Android App called Geo Chart++ in July 2013.

Link to Google Chart Tools “spiritual home” … via Google.
Link to Google Chart Tools Intensity Map information … via Google.

Link to some downloadable PHP programming code … rename to intensity_chart.php.

If this was interesting you may be interested in this too.


If this was interesting you may be interested in this too.


If this was interesting you may be interested in this too.


If this was interesting you may be interested in this too.

This entry was posted in eLearning, Operating System, Tutorials and tagged , , , , , , . Bookmark the permalink.

15 Responses to Windows Command Line sort Tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *