Sort outer and inner strings alphabetically

Discussion in 'Javascript' started by Brion, Sep 2, 2007.

  1. Brion

    Brion Guest

    Is it possible to sort outer and inner strings at once?
    The sorted output should look like the following:

    <category>Bars</category>
    <name>Alpha Bar</name>
    <name>Beta Bar</name>

    <category>Cafes</category>
    <name>Ara Cafe</name>
    <name>Zeta Cafe</name>


    Categories and names are both properties of one and the same array
    element.
    I have the following compare function to sort the outer categories.


    function compareCats(a, b) {
    a = a.category;
    b = b.category;
    if(a == b) return 0;
    else if(a > b) return 1;
    else return -1;
    }

    This is working fine.
    But I really would like to include the inner names in the compare
    function without changing the structure of the array - if it could be
    possible. If not - what would be an efficient way to solve the problem?
     
    Brion, Sep 2, 2007
    #1
    1. Advertisements

  2. Define "outer string" and "inner string".
    That would be XML markup. Where is the relevance to your question?
    What is an "outer category"?
    You have yet to present the array you are operating on.
    It probably is.
    No input, no output.


    PointedEars
     
    Thomas 'PointedEars' Lahn, Sep 2, 2007
    #2
    1. Advertisements

  3. Brion

    Brion Guest


    Only as slightly OT as the above answers:



    I thought you would read the whole question. It is all clearly
    defined:

    Categories = outer strings
    names = inner strings

    I also thought I only will get an answer by someone who is not
    clueless about the subject.
    But it seems my assumptions were all definetely wrong.

    Bye guys - never mind - I'll try it myself
     
    Brion, Sep 3, 2007
    #3
  4. Brion

    RobG Guest

    No, it isn't. You provided a data structure that, to me, is not
    consistent with your question, javascript or HTML. It might be XML,
    but you say it is the final output. The names attributes are only
    related to the category elements by position, should the output be:

    <category>
    <name>...</name>
    <name>...</name>
    </category>

    <category>
    <name>...</name>
    <name>...</name>
    </category>

    or similar?

    What subject? You mentioned sorting an array to get the above output
    without ever specifying what the structure of the original array (or
    input data) was.
    Apparently, I can't answer that since I don't know what your
    assumptions were regarding "the subject"

    If you post a bit more information you might get a better answer - or
    not. :)
     
    RobG, Sep 3, 2007
    #4
  5. Brion

    Brion Guest


    It's an old well proven trick to quarrel about definitions when
    solutions are either completely unknown or not accepted.

    I agree: There is no answer possible if one of participants even
    rejects that there is a well known subject they're talking about. And
    the questioner is joshed a priori. But that seems to be the obvious
    intention of the responders of this group.
     
    Brion, Sep 3, 2007
    #5
  6. Brion

    pr Guest

    Guessing that your input looks like this:

    var arr = [{category:"Cafes", names:["Zeta Cafe", "Ara Cafe"]},
    {category:"Bars", names:["Beta Bar", "Alpha Bar"]}];

    then the answer may be:

    arr.sort(compareCats);

    for(i = 0; i < arr.length; i++) {
    arr.names.sort();
    }
     
    pr, Sep 3, 2007
    #6
  7. Brion

    RobG Guest

    Not at all. If you answer the questions I asked, I will give serious
    consideration to providing an answer or suggestions. If you provide a
    minimal, stand-alone example of what you have now, then I'm sure
    you'll get good responses (and maybe a few not-so-good, but hey, this
    is Usenet!). :)
     
    RobG, Sep 3, 2007
    #7
  8. Brion

    RobG Guest

    or:

    var arr = [
    {Cafes:["Zeta Cafe", "Ara Cafe"]},
    {Bars:["Beta Bar", "Alpha Bar"]}
    ];

    or maybe:

    var arr = [
    'Cafes.Zeta','Cafes.Ara',
    'Bar.Beta', 'Bar.Alpha'
    ];


    The person who knows won't say.
     
    RobG, Sep 3, 2007
    #8
  9. Brion

    Brion Guest


    This is the structure of the array:

    var arr = [{ "category": "bar", "name": "Alpha Bar" },
    { "category": "bar", "name": "Beta Bar"},
    { "category": "cafe", "name": "Ara Cafe" },
    { "category": "cafe", "name": "Zeta Cafe" }
    ];
     
    Brion, Sep 3, 2007
    #9
  10. Brion

    pr Guest

    Ah. In that case:

    function compareCats(a, b) {
    if(a.category == b.category) {
    return (a.name < b.name ? -1 : (a.name > b.name));
    } else {
    return (a.category < b.category ? -1 : (a.category > b.category));
    }
    }
     
    pr, Sep 3, 2007
    #10
  11. Brion

    Brion Guest

    Thats a good solution. Thank you very much.
    :)
     
    Brion, Sep 3, 2007
    #11
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.