标签: sorting

Javascript recursively order object and nested objects as well as arrays

I am trying to get the same results as pythons json.dumps() with sort by keys enabled. This is preformed as a pre-request script for Postman to generate the request hash. The output needs to be sorted valid json which is used as input for hashing. I am new to javascript and see many old answers claiming that objects in javascript cannot be sorted. However there must be a solution to generate the hash given the criteria.

  • The object structure cannot …

javascript python sorting recursion

1
推荐指数
1
解决办法
2130
查看次数

如何对可迭代对象进行排序

我有一个实现 Iterable 的自定义列表(不是集合!!)。我没有实现 List ,因为这会带来太多我不想要的方法,而且我不需要。

关于我的 CustomList 的更多信息:

  • 所有成员都是最终的
  • 每个 CustomList 都可以有一个“SubList”,它是另一个 CustomList
  • 每个 CustomList 必须只有一个 MyThing
  • 示例:假设 A 是 MyThing 的对象,B 是 MyThing 的对象,可以有一个只有 A(我们称之为 AList)的 CustomList 和一个带有 Item B 和 AList 作为 Sublist 的 CustomList。
  • CustomList 有一个可以接受 MyThings 数组的构造函数

所以这是我的班级:

public class CustomList implements Iterable<MyThing> {

   private final CustomList sublist;

   private final MyThing thing;

   // getters and other methods
}
Run Code Online (Sandbox Code Playgroud)

我有一个

Comparator<MyThing>
Run Code Online (Sandbox Code Playgroud)

我的事情:

public class MyThing {

   private final IntArrayList values;
   // more members and methods
}
Run Code Online (Sandbox Code Playgroud)

我想获得一个排序副本作为我的 …

java sorting iterable

1
推荐指数
1
解决办法
1万
查看次数

首先按降序对 Yii2 Gridview 进行排序

我有一个由 Yii2 GridView 呈现的表格。表头包含按日期排序的链接。如果我单击它,它首先按升序对表格进行排序,然后按降序对第二次单击进行排序。但我想在第一次点击时降序。

我在搜索控制器的搜索方法 (asc->SORT_DESC) 中使用 hack 解决了它:

   $dataProvider->sort->attributes['updated_at'] = [ 
      'asc'  => [$this->tablename() . '.updated_at' => SORT_DESC ], 
      'desc' => [$this->tablename() . '.updated_at' => SORT_ASC], 
   ]; 
Run Code Online (Sandbox Code Playgroud)

有更好的解决方案吗?

sorting gridview yii2

1
推荐指数
1
解决办法
5978
查看次数

Python:按值对字典进行排序,同时按字母顺序维护以防止出现关系

因此,如果项目值相同,我想按值对字典进行排序并按字母顺序维护以防止出现联系。但是,python字典没有按字母顺序保存数据,如何解决?先感谢您!

这是我的代码:

from collections import Counter
test = "betty bought a bit of butter but the butter was bitter"
Counter(test.split()).most_common(3)
Run Code Online (Sandbox Code Playgroud)

输出:

[('butter', 2), ('a', 1), ('bitter', 1)]
Run Code Online (Sandbox Code Playgroud)

所需的输出应该是:

[('butter', 2), ('a', 1), ('betty', 1)]
Run Code Online (Sandbox Code Playgroud)

因为“苦味”应该按字母顺序排在“贝蒂”之后。

python sorting dictionary

1
推荐指数
1
解决办法
620
查看次数

RuleBasedCollat​​or 不适用于 Android?

我尝试RuleBasedCollator在 android 上对字符串进行排序。但是没有一个来自https://developer.android.com/reference/java/text/RuleBasedCollat​​or.html 的示例 在 android 5 上不起作用并抛出异常。

    Caused by: java.text.ParseException: ucol_openRules failed: U_INVALID_FORMAT_ERROR (at offset -1)   
   at java.text.RuleBasedCollator.<init>(RuleBasedCollator.java:114)  
Run Code Online (Sandbox Code Playgroud)

例如,代码会抛出异常

 String simple = "< a< b< c< d";
 RuleBasedCollator mySimple = new RuleBasedCollator(simple);
Run Code Online (Sandbox Code Playgroud)

我还尝试了文章http://envyandroid.com/rulebasedcollat​​or-crashes-android-5-0/ 中的提示,但仍然无法正常工作

sorting android

1
推荐指数
1
解决办法
220
查看次数

使用 JQuery 按属性对 DIV 进行排序

我正在尝试使用按钮对许多DIV使用自定义属性进行排序。我正在关注此示例,但无法使其与自定义属性一起使用。我知道我访问属性的方式可能有问题,但不确定如何解决。

我的HTML是:

<button id="opBnt">OP</button>      
<button id="inBnt">IN</button>
<div id="collapsible" class="panel-collapse">                   
    <div id="1" class="superduper" data-sort-op="0.4" data-sort-in="0.1">a</div>
    <div id="2" class="superduper" data-sort-op="0.2" data-sort-in="0.8">b</div>
    <div id="3" class="superduper" data-sort-op="0.3" data-sort-in="0.3">c</div>
</div>
Run Code Online (Sandbox Code Playgroud)

并且JS:

var $divs = $("div.superduper");
$('#opBnt').on('click', function () {
    var opOrder = $divs.sort(function (a, b) {
        return $(a).getAttribute('data-sort-op') > $(b).getAttribute('data-sort-op');
    });
    $("#collapsible").html(opOrder);
});

$('#inBnt').on('click', function () {   
    var inOrder = $divs.sort(function (a, b) {
        return $(a).getAttribute('data-sort-in') > $(b).getAttributel('data-sort-in');
    });
    $("#collapsible").html(inOrder);
});
Run Code Online (Sandbox Code Playgroud)

小提琴

html javascript sorting jquery

1
推荐指数
1
解决办法
1668
查看次数

如何按字母顺序对 ObservableCollection 进行排序?

我需要ObservableCollection按字母顺序对after populatior 进行排序,假设里面有这个模型:

class Nation
{
    public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

这是我的ObservableCollection:

ObservableCollection<Nation> ob = new ObservableCollection<Nation>();
Run Code Online (Sandbox Code Playgroud)

假设在我里面我有这个订单的三个项目:

  1. 英国
  2. 阿富汗
  3. 意大利

我需要按字母顺序排序,所以我尝试:

ob.OrderBy(c => c.Name);
Run Code Online (Sandbox Code Playgroud)

但我没有效果,应该是:

  1. 阿富汗
  2. 英国
  3. 意大利

我做错了什么?

c# sorting

1
推荐指数
1
解决办法
7889
查看次数

Go:循环排序数组,sort.Sort 用作值

如何循环排序数组?

我收到“sort.Sort used as value”错误:https : //play.golang.org/p/HP30OyJVrz

package main

import (
    "fmt"
    "sort"
)

type Person struct {
    Name string
    Age  int
}

func (p Person) String() string {
    return fmt.Sprintf("%s: %d", p.Name, p.Age)
}

// ByAge implements sort.Interface for []Person based on
// the Age field.
type ByAge []Person

func (a ByAge) Len() int           { return len(a) }
func (a ByAge) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a ByAge) Less(i, j int) bool …
Run Code Online (Sandbox Code Playgroud)

arrays sorting go

1
推荐指数
1
解决办法
1392
查看次数

Pandas 多索引数据框获取每个排序组的前 5 行

我有一个多索引数据帧,如下所示:

在此处输入图片说明

我想对每个海报组(降序)进行排序并获得前 5 名。如果发帖数少于5,则删除记录。

python sorting group-by dataframe pandas

1
推荐指数
1
解决办法
1181
查看次数

php asort不适用于文本字符串数组

我有一个数组

print_r($RL_all_years_unique_0);
Run Code Online (Sandbox Code Playgroud)

给出:

Array ( [0] => kWh Electricity [1] => Kg Refrigerants [2] => Gallons Diesel [3] => Indirect VMT [4] => Short Tons Solid Waste [5] => Gallons Propane [6] => Cubic Ft Nat Gas [7] => Gallons 87 Gasoline [8] => WasteWater - kg N [9] => WasteWater - Pop ) 
Run Code Online (Sandbox Code Playgroud)

然后我做:

asort($RL_all_years_unique_0);
print_r($RL_all_years_unique_0);
Run Code Online (Sandbox Code Playgroud)

给出:

Array ( [6] => Cubic Ft Nat Gas [7] => Gallons 87 Gasoline [2] => Gallons Diesel [5] => Gallons Propane [3] …
Run Code Online (Sandbox Code Playgroud)

php arrays sorting text

1
推荐指数
1
解决办法
894
查看次数

标签 统计

sorting ×10

python ×3

arrays ×2

javascript ×2

android ×1

c# ×1

dataframe ×1

dictionary ×1

go ×1

gridview ×1

group-by ×1

html ×1

iterable ×1

java ×1

jquery ×1

pandas ×1

php ×1

recursion ×1

text ×1

yii2 ×1