# Sort outer and inner strings alphabetically

B

#### Brion

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?

T

#### Thomas 'PointedEars' Lahn

Brion said:
Is it possible to sort outer and inner strings at once?

Define "outer string" and "inner string".
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?
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"?
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.
- if it could be possible.

It probably is.
If not - what would be an efficient way to solve the problem?

No input, no output.

PointedEars

B

#### Brion

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
But it seems my assumptions were all definetely wrong.

Bye guys - never mind - I'll try it myself

R

#### RobG

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

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

not.

B

#### Brion

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.

P

#### pr

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

arr.sort(compareCats);

for(i = 0; i < arr.length; i++) {
arr.names.sort();
}

R

#### RobG

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

R

#### RobG

Brion said:
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.

B

#### Brion

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

P

#### pr

Brion said:
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));
}
}