By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,955 Members | 1,477 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,955 IT Pros & Developers. It's quick & easy.

Sort outer and inner strings alphabetically

P: n/a
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?

Sep 2 '07 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Thomas 'PointedEars' Lahn said the following on 9/2/2007 4:34 PM:
Brion wrote:
>Is it possible to sort outer and inner strings at once?

Define "outer string" and "inner string".
Are you that far out in space?
>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>

That would be XML markup. Where is the relevance to your question?
Iff if it were presented as markup.
>Categories and names are both properties of one and the same array
element.
I have the following compare function to sort the outer categories.

What is an "outer category"?
<eyeroll>
>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

You have yet to present the array you are operating on.
Just based on the original post, it is trivial to get to an array
definition. Please don't tell me that you are that anal.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Sep 2 '07 #2

P: n/a
On Sep 2, 10:34 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
Define "outer string" and "inner string".

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

Sep 3 '07 #3

P: n/a
On Sep 3, 4:27 pm, Brion <p...@arcor.dewrote:
On Sep 2, 10:34 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
Define "outer string" and "inner string".

Only as slightly OT as the above answers:

I thought you would read the whole question. It is all clearly
defined:
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?

Categories = outer strings
names = inner strings

I also thought I only will get an answer by someone who is not
clueless about the subject.
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.
But it seems my assumptions were all definetely wrong.
Apparently, I can't answer that since I don't know what your
assumptions were regarding "the subject"

Bye guys - never mind - I'll try it myself
If you post a bit more information you might get a better answer - or
not. :-)
--
Rob

Sep 3 '07 #4

P: n/a
On Sep 3, 8:57 am, RobG <rg...@iinet.net.auwrote:
Apparently, I can't answer that since I don't know what your
assumptions were regarding "the subject"

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.

Sep 3 '07 #5

P: n/a
On Sep 3, 6:16 pm, Brion <p...@arcor.dewrote:
On Sep 3, 8:57 am, RobG <rg...@iinet.net.auwrote:
Apparently, I can't answer that since I don't know what your
assumptions were regarding "the subject"

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.
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!). :-)

--
Rob


Sep 3 '07 #6

P: n/a
pr
Brion wrote:
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?
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[i].names.sort();
}
Sep 3 '07 #7

P: n/a
On Sep 3, 9:06 pm, pr <p...@porl.globalnet.co.ukwrote:
Brion wrote:
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?

Guessing that your input looks like this:

var arr = [{category:"Cafes", names:["Zeta Cafe", "Ara Cafe"]},
{category:"Bars", names:["Beta Bar", "Alpha Bar"]}];
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.

--
Rob

Sep 3 '07 #8

P: n/a

RobG wrote:
The person who knows won't say.

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" }
];

Sep 3 '07 #9

P: n/a
pr
Brion wrote:
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" }
];
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));
}
}
Sep 3 '07 #10

P: n/a

pr wrote:
Ah. In that case:
Thats a good solution. Thank you very much.
:-)

Sep 3 '07 #11

This discussion thread is closed

Replies have been disabled for this discussion.