按字母顺序排列外部和内部字符串 [英] Sort outer and inner strings alphabetically

查看:77
本文介绍了按字母顺序排列外部和内部字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以一次对外部和内部字符串进行排序?

已排序的输出应如下所示:


< category> Bars< / category>

< name> Alpha Bar< / name>

< name> Beta Bar< / name>


< category> Cafes< / category>

< name> Ara Cafe< / name>

< name> Zeta Cafe< / name>

类别和名称都是同一个数组的属性

元素。

我有以下比较函数来排序外部类别。

函数compareCats(a,b){

a = a.category;

b = b.category;

if(a = = b)返回0;

否则如果(ab)返回1;

否则返回-1;

}


这样工作正常。

但是我真的想在比较

函数中包含内部名称而不改变数组的结构 - 如果它可能是

可能的。如果不是 - 什么是解决问题的有效方法?

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?

推荐答案

Thomas''PointedEars''Lahn说以下内容2007年9月2日下午4:34:
Thomas ''PointedEars'' Lahn said the following on 9/2/2007 4:34 PM:

Brion写道:
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?


>排序的输出应如下所示:

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

< category> Cafes< / category>
< name> Ara Cafe< / name>
< name> Zeta Cafe< / name>
>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>



这将是XML标记。与您的问题的相关性在哪里?


That would be XML markup. Where is the relevance to your question?



Iff如果它被显示为标记。

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>

<eyeroll>


> function compareCats(a ,b){
a = a.category;
b = b.category;
如果(a == b)返回0;
如果(ab)返回1;
返回-1;
}

这个工作正常。
但我真的想在比较
函数中包含内部名称而不用更改数组的结构
>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.



只是基于原始帖子,获得一个数组

定义是微不足道的。请不要告诉我你是那个肛门。


-

兰迪

机会有利于准备好的心灵

comp.lang.javascript常见问题 - http:// jibbering。 com / faq / index.html

Javascript最佳实践 - http://www.javascriptToolbox.com/bestpractices/


9月2日晚上10点34分,Thomas''PointedEars''Lahn< ; PointedE ... @ web.de>

写道:
On Sep 2, 10:34 pm, Thomas ''PointedEars'' Lahn <PointedE...@web.de>
wrote:

定义外部字符串和内部字符串。
Define "outer string" and "inner string".



只有与上述答案稍微加点:


我以为你会读完整个问题。这一切都很清楚

定义:


类别=外部字符串

names =内部字符串


我还以为我只会得到一个对这个问题毫无瑕疵的人的回答。

但似乎我的假设都是错误的。


再见家伙 - 没关系 - 我会亲自尝试


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


9月3日,4:27下午,Brion< p ... @ arcor.dewrote:
On Sep 3, 4:27 pm, Brion <p...@arcor.dewrote:

9月2日晚上10点34分,Thomas''PointedEars''Lahn< PointedE。 .. @ web.de>

写道:
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:



不,它不是。您提供了一个数据结构,对我而言,与您的问题,javascript或HTML不一致。它可能是XML,

,但你说它是最终输出。如果输出为:


< category>

,则名称属性仅与按位置的类别元素相关的
< name> ...< / name>

< name> ...< / name>

< / category>


< category>

< name> ...< / name>

< name> ...< / name> ;

< / category>


或类似?

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.



显然,我无法回答这个问题,因为我不知道你的'b $ b'假设是关于主题的。

Apparently, I can''t answer that since I don''t know what your
assumptions were regarding "the subject"


Bye guys - 没关系 - 我会亲自尝试
Bye guys - never mind - I''ll try it myself



如果你发布了一些信息,你可能会得到更好的答案 - 或者

不是。 :-)

-

Rob

If you post a bit more information you might get a better answer - or
not. :-)
--
Rob


这篇关于按字母顺序排列外部和内部字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆