首页 \ 问答 \ SQL按字典顺序排序,但首先排序较长的字符串(SQL Order by lexicographical order, but with longer string first)

SQL按字典顺序排序,但首先排序较长的字符串(SQL Order by lexicographical order, but with longer string first)

在SQL Server中,我有一个包含要排序的列的表。 如果我应用通常的ORDER BY ,它会按照预期执行以下操作:

banana
bananaphone
car
carpet
dishes
//etc

不过,我想让他们按照其他方式排序:

bananaphone
banana
carpet
car
dishes
//etc

仍然按字母顺序排列,但如果字母匹配且一个比另一个长,则首先放置较长的一个。

是否有可能在T-SQL中做这样的查询?

我甚至不知道这个排序算法是否有名字,我找不到它。

请注意,这篇文章中的数据只是一个例子,我将存储用户定义的数据。


In SQL Server, I have a table with a column I want to sort. If I apply the usual ORDER BY, it does the following, as expected:

banana
bananaphone
car
carpet
dishes
//etc

However, I would like to have them sorted this other way:

bananaphone
banana
carpet
car
dishes
//etc

Still lexicographically, but if the letters match and one is longer than the other, place the longer one first.

Is it possible to do such a query in a T-SQL?

I don't even know if this sorting algorithm has a name, I couldn't find it.

Please note that the data in this post is just an example, and I will be storing user-defined data.


原文:https://stackoverflow.com/questions/29686569
更新时间:2022-12-26 10:12

最满意答案

您可以使用.siblings()和选择器,如下所示:

$(myContextNode).siblings("span.myClass")

You can use .siblings() with a selector, like this:

$(myContextNode).siblings("span.myClass")

相关问答

更多
  • 使用jQuery .siblings()来选择匹配的同级。 $(this).siblings('.bidbutton'); Use jQuery .siblings() to select the matching sibling. $(this).siblings('.bidbutton');
  • 您可以使用eq方法将匹配的元素集合减少到特定索引处的元素: $(this).siblings(".bar").eq(0).text() 这将选择集合中的第一个元素。 在你的情况下,你可以简单地使用prev ,因为你要找的元素直接来自clicked元素之前: $(this).prev(".bar").text() 你使用的数组表示法的问题是它返回的是包含在jQuery对象中的实际DOM节点,并且没有text方法。 eq是执行此操作的等效jQuery方法。 You can use the eq method ...
  • 好吧,确定...只需访问父母,然后再访问孩子。 node.parentNode.childNodes[] 或...使用jQuery: $('#innerId').siblings() 编辑:Cletus一如既往的鼓舞人心。 我进一步挖 这是jQuery本质上获得兄弟姐妹的方式: function getChildren(n, skipMe){ var r = []; for ( ; n; n = n.nextSibling ) if ( n.nodeType == 1 ...
  • 你如何使用xpath? 如果你只是循环超类,从而知道它们的位置,并且可以动态地构建xpath,则可以简单地计数前面的subercategories。 //li[@class="supercategory"][position()=2]/following-sibling::li[@class ="subcategory"][count(preceding-sibling::li[@class='supercategory']) = 2] How are you using the xpath? If yo ...
  • 以下是一些测试用例,为您提供一般概念: http://jsperf.com/next-vs-nextall-frist/2 http://jsperf.com/nextall-first-vs-nextuntil-last-next 同样非常有趣的是next()vs getelementbyid的比较: http://jsperf.com/next-vs-document-getelementbyid/2 您可以采用任何这些测试用例并添加自己的其他测试。 Here is a few test-cases f ...
  • 您需要获取所选节点的父节点,然后获取该父节点的子节点(因此是所选节点的兄弟节点)。 试试这段代码: