Sorting lines descending on one number and ascending on a second number

Sorting lines descending on one number and ascending on a second number

24
Basic UserBasic User
24

    Oct 27, 2013#1

    I need a SSS (Simple Sort Script). :D

    From this:

    Code: Select all

    01 = 502
    02 = 466
    03 = 467
    04 = 497
    05 = 467
    06 = 471
    07 = 465
    08 = 486
    09 = 439
    10 = 495
    11 = 487
    12 = 484
    13 = 484
    14 = 492
    15 = 501
    16 = 479
    17 = 451
    18 = 501
    19 = 474
    20 = 472
    21 = 459
    22 = 462
    23 = 471
    To this:

    Code: Select all

    01 = 502
    15 = 501
    18 = 501
    04 = 497
    10 = 495
    14 = 492
    11 = 487
    08 = 486
    12 = 484
    13 = 484
    16 = 479
    19 = 474
    20 = 472
    06 = 471
    23 = 471
    03 = 467
    05 = 467
    02 = 466
    07 = 465
    22 = 462
    21 = 459
    17 = 451
    09 = 439

    6,686585
    Grand MasterGrand Master
    6,686585

      Oct 29, 2013#2

      With the sort settings in attached screen shot you can sort the lines descending depending on the number of the equal sign. This sort produces

      Code: Select all

      01 = 502
      18 = 501
      15 = 501
      04 = 497
      10 = 495
      14 = 492
      11 = 487
      08 = 486
      13 = 484
      12 = 484
      16 = 479
      19 = 474
      20 = 472
      23 = 471
      06 = 471
      05 = 467
      03 = 467
      02 = 466
      07 = 465
      22 = 462
      21 = 459
      17 = 451
      09 = 439
      But that result is not exactly of what you want as for example line 2 and 3 are in wrong order.

      What you want is really a very special sort and therefore not a simple sort. The sorting criterias are:
      1. If string (number) after equal sign on current line is greater than the string (number) after equal sign on the line compared with, put the current line before the compared line.
      2. If string (number) after equal sign on current line is smaller than the string (number) after equal sign on the line compared with, put the current line after the compared line.
      3. If string (number) after equal sign on current line is equal the string (number) after equal sign on the line compared with, one more sort must be used.
        1. If string (number) before equal sign on current line is greater than the string (number) before equal sign on the line compared with, put the current line after the compared line.
        2. If string (number) before equal sign on current line is smaller than the string (number) before equal sign on the line compared with, put the current line before the compared line.
        3. If string (number) before equal sign on current line is equal the string (number) before equal sign on the line compared with, do not change the sort order (duplicate line).
      Such a sort is possible with a script and a custom sort function, but I do not yet have time to write it. Perhaps there is another forum user who has some time to write this little script for you.
      advanced_descending_sort.png (2.67KiB)
      Advanced sort options for descending sort of the lines depending on number after equal sign.
      Best regards from an UC/UE/UES for Windows user from Austria

      24
      Basic UserBasic User
      24

        Oct 30, 2013#3

        Thanks, it works.

        Not 100%, but can do the rest manually.

        6,686585
        Grand MasterGrand Master
        6,686585

          Nov 01, 2013#4

          Here is the script which produces the result you want. It does the primary descending sort and the secondary ascending sort on lines with equal second number.

          Code: Select all

          // Function DesAscSort sorts lines primary in descending order based on
          // a number after the first equal sign. If two lines have the same number
          // after the first equal sign, the lines are sorted secondary in ascending
          // order according to the number before the first equal sign. Lines without
          // an equal sign like the empty string at end of the array if file ends with
          // a line termination are moved to end of the file with keeping their order.
          
          /* Example:
          
          11 = 487
          13 = 484
          There is no equal sign.
          12 = 484
          One more line without equal sign.
          14 = 492
          
          becomes
          
          14 = 492
          11 = 487
          12 = 484
          13 = 484
          There is no equal sign.
          One more line without equal sign.
          
          */
          
          function DesAscSort (sLineA, sLineB)
          {
             var nEqualSignPosA = sLineA.indexOf('=');
             var nEqualSignPosB = sLineB.indexOf('=');
          
             // Does line A contain an equal sign?
             if (nEqualSignPosA < 0)
             {
                // No! Does line B contain an equal sign?
                if (nEqualSignPosB < 0)
                {
                   return 0;   // No, do not change order of the lines.
                }
                return 1;      // Yes, put line A after line B.
             }
          
             // Yes, line A contains an equal sign.
             // Does line B contain an equal sign?
             if (nEqualSignPosB < 0)
             {
                return -1;  // No, put line A before line B.
             }
          
             // Both lines contain an equal sign.
          
             // It is expected that there is an integer number left the first equal
             // sign and another integer number right the equal sign and the number
             // of digits is constant on all lines. There is no check made if those
             // conditions are met by all lines containing an equal sign.
          
             // Get from both lines the number after the equal sign.
             var sNumberA = sLineA.replace(/^.*?=[ \t]*(\d+).*$/,"$1");
             var sNumberB = sLineB.replace(/^.*?=[ \t]*(\d+).*$/,"$1");
          
             // Is the string (number) of line A greater the string (number) of
             // line B, line A must be put before line B in the array (file).
             if (sNumberA > sNumberB)
             {
                return -1;
             }
          
             // Is the string (number) of line B greater the string (number) of
             // line A, line A must be put after line B in the array (file).
             else if (sNumberA < sNumberB)
             {
                return 1;
             }
          
             // The numbers after the first equal sign are identical on both lines.
             // Get from both lines the number left of the first equal sign.
             sNumberA = sLineA.replace(/^(\d+).+$/,"$1");
             sNumberB = sLineB.replace(/^(\d+).+$/,"$1");
          
             // Is the string (number) of line A greater the string (number) of
             // line B, line A must be put after line B in the array (file).
             if (sNumberA > sNumberB)
             {
                return 1;
             }
          
             // Is the string (number) of line B greater the string (number) of
             // line A, line A must be put before line B in the array (file).
             else if (sNumberA < sNumberB)
             {
                return -1;
             }
          
             return 0;   // Both numbers are identical, no change in order required.
          }
          
          
          if (UltraEdit.document.length > 0)  // Is any file opened?
          {
             // Define environment for this script.
             UltraEdit.insertMode();
             UltraEdit.columnModeOff();
          
             // Determine line terminator type (DOS/Windows or UNIX).
             var sLineTerm = (UltraEdit.activeDocument.lineTerminator != 1) ? "\r\n" : "\n";
          
             UltraEdit.activeDocument.selectAll();
             if (UltraEdit.activeDocument.isSel())  // Is file not empty?
             {
                UltraEdit.selectClipboard(9);
                UltraEdit.activeDocument.copy();
          
                // Get the clipboard content into an array of strings
                // with each string being a line in active file.
                var asLines = UltraEdit.clipboardContent.split(sLineTerm);
          
                // Sort the lines with the special sorting function.
                asLines.sort(DesAscSort);
          
                // Build the block in user clipboard 9 and paste it
                // into the file overwriting the entire selection.
                UltraEdit.clipboardContent = asLines.join(sLineTerm);
                UltraEdit.activeDocument.paste();
          
                // Clear user clipboard 9, select Windows clipboard and
                // exit script with setting caret to top of the file.
                UltraEdit.clearClipboard();
                UltraEdit.selectClipboard(0);
                UltraEdit.activeDocument.top();
             }
          }
          Best regards from an UC/UE/UES for Windows user from Austria