sorting???

This is a discussion on sorting??? within the Linux Programming forums, part of the Platform Specific Boards category; Hi.....i was just wondering how do i use the sort command to sort the following format from highest to lowest ...

  1. #1
    kiss2rvp
    Guest

    Exclamation sorting???

    Hi.....i was just wondering how do i use the sort command to sort the following format from highest to lowest with the number the key but still keep the fields belonging to that key with it in the final output???

    format i have now:

    2
    A
    B
    C
    1
    a
    r
    g
    3
    h
    r
    t
    and final output should be :

    3
    h
    r
    t
    2
    A
    B
    C
    1
    a
    r
    g

    thanks in advance.....

  2. #2
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    you need a pattern first otherwise you will just end up manually placing the chars where they belong in the array
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  3. #3
    Registered User stautze's Avatar
    Join Date
    Apr 2002
    Posts
    195
    if every number has three letters after it then you can use a really simple structure like this:

    typedef struct entry {
    int key;
    char letter1;
    char letter2;
    char letter3;
    } Entry;

    Better though, would be this where the key could have any number of letters that are associated with it:

    typedef struct entry {
    int key;
    char *letters;
    } Entry;
    'During my service in the United States Congress, I took the initiative in creating the Internet.' - Al Gore, March 9, 1999: On CNN's Late Edition

  4. #4
    Registered User alex's Avatar
    Join Date
    Sep 2001
    Posts
    132
    Hi!

    First, I would replicate the key in front of each line (using awk, for example).
    Then do a stable sort on this key (treating it as a number, and reversing the sort order)
    Finally, I would kill the superfluous key (again using awk).

    cat unsortedfile | awk '{if($1==$1+0) key=$1; print key, $1}' | sort -s -k 1nr | awk '{print $2}' > sortedfile

    Have fun...
    alex

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 03:14 AM
  2. sorting structure members using pointers
    By robstr12 in forum C Programming
    Replies: 5
    Last Post: 07-25-2005, 06:50 PM
  3. Sorting words with a fast, effincient sorting method
    By Unregistered in forum C++ Programming
    Replies: 19
    Last Post: 07-12-2002, 05:21 PM
  4. Still Needing Help : selection sorting
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 10-14-2001, 09:41 PM
  5. selection sorting
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 10-13-2001, 09:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21