Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Sort outer and inner strings alphabetically

Reply
Thread Tools

Sort outer and inner strings alphabetically

 
 
Brion
Guest
Posts: n/a
 
      09-02-2007
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?

 
Reply With Quote
 
 
 
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      09-02-2007
Brion wrote:
> 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
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
 
Reply With Quote
 
 
 
 
Brion
Guest
Posts: n/a
 
      09-03-2007
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

 
Reply With Quote
 
RobG
Guest
Posts: n/a
 
      09-03-2007
On Sep 3, 4:27 pm, Brion <p...@arcor.de> wrote:
> 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

 
Reply With Quote
 
Brion
Guest
Posts: n/a
 
      09-03-2007
On Sep 3, 8:57 am, RobG <rg...@iinet.net.au> wrote:

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

 
Reply With Quote
 
pr
Guest
Posts: n/a
 
      09-03-2007
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();
}
 
Reply With Quote
 
RobG
Guest
Posts: n/a
 
      09-03-2007
On Sep 3, 6:16 pm, Brion <p...@arcor.de> wrote:
> On Sep 3, 8:57 am, RobG <rg...@iinet.net.au> wrote:
>
> > 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




 
Reply With Quote
 
RobG
Guest
Posts: n/a
 
      09-03-2007
On Sep 3, 9:06 pm, pr <p...@porl.globalnet.co.uk> wrote:
> 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

 
Reply With Quote
 
Brion
Guest
Posts: n/a
 
      09-03-2007

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

 
Reply With Quote
 
pr
Guest
Posts: n/a
 
      09-03-2007
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));
}
}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
iterate over a series of alphabetically increasing strings Vellingiri Arul Ruby 3 09-19-2007 11:16 AM
hot to align inner table cols with outer table cols phl HTML 2 05-23-2006 10:58 AM
inner class, explicit outer class method call Yamin Java 4 10-24-2004 06:17 AM
Access outer / inner class variables query lonelyplanet999 Java 3 11-18-2003 07:16 PM
How to access inner classes variables & methods from outer classes lonelyplanet999 Java 1 11-13-2003 01:54 PM



Advertisments