我有一个对象列表,想根据实例函数的返回值对它们进行排序。有两种方法可以做到这一点
from operator import methodcaller
l.sort(key=lambda x:x.f())
l.sort(key=methodcaller('f'))
Run Code Online (Sandbox Code Playgroud)
一种方式比另一种更好吗?或者这只是个人喜好?
我有这个清单:
[['Nom', 'Francais', 'Anglais', 'Maths'], ['Catherine', '9', '17', '9'], ['Karim', '12', '15', '11'], ['Rachel', '15', '15', '14'], ['Roger', '12', '14', '12'], ['Gabriel', '7', '13', '8'], ['Francois', '14', '8', '15'], ['Henri', '10', '12', '13'], ['Stephane', '18', '12', '8'], ['Karine', '9', '10', '10'], ['Marie', '10', '10', '10'], ['Claire', '15', '9', '12'], ['Marine', '12', '9', '12']]
Run Code Online (Sandbox Code Playgroud)
我想用名称对其进行排序(或者,换句话说,按列表中每个列表的 [0] 元素的字母顺序排序),但我不希望第一个列表 ( )['Nom', 'Francais', 'Anglais', 'Maths']与其他人,怎么能这么做呢?
多谢 !
首先,我注意到在元组排序列表上发布了很多问题,但是在查看了一些帖子后,我没有看到任何具有这种特定列表格式的问题。对于可能出现的重复问题,提前表示歉意,但我认为这应该很简单。
让元组列表如下:
my_list = [(('G', 'J', 'I'), 1.0), (('E', 'H', 'F'), 1.0), (('F', 'H', 'G'), 0.8889), (('I', 'K', 'J'), 0.8889), (('H', 'I', 'G'), 0.8889), (('H', 'J', 'I'), 0.875)]
Run Code Online (Sandbox Code Playgroud)
请注意,列表中的每个元组由 1:另一个长度为 3、有 3 个字母的元组和 2:一个浮点数组成。我的排序目标很简单:首先按浮点数对元组列表进行排序,第二按长度 3 元组中的第一个字母排序,打破浮点数中的任何联系,第三按按 length-3 元组中的第二个字母排序。所需的输出将是:
sorted_list = [(('E', 'H', 'F'), 1.0), (('G', 'J', 'I'), 1.0), (('F', 'H', 'G'), 0.8889), (('H', 'I', 'G'), 0.8889), (('I', 'K', 'J'), 0.8889), (('H', 'J', 'I'), 0.875)]
Run Code Online (Sandbox Code Playgroud)
在这个特定的示例中,对第二个字母进行排序以打破第一个字母的联系并没有出现,尽管它在我的较大数据集中出现了。
谢谢!
尝试用 Swift 编写以下快速排序算法有一段时间了,但无法解决该问题。[快速排序,因为数组中有大约 15,000 个实际值]。问题是只有数组的左半部分是有序的(见图),并且该方法永远不会退出(无限循环)。从http://www.java2novice.com/java-sorting-algorithms/quick-sort/进行 Java 转换(在 Java 中测试并确实有效)。无法解决错误。
var arr = [Int]()
var length = 0
override func viewDidLoad()
{
super.viewDidLoad()
arr = [5,2,4,7,2,1,3,9,10,11,7,8]
print(arr)
sort(inputArr: &arr);
print(arr)
}
func sort(inputArr: inout [Int])
{
if (inputArr.count == 0) || (inputArr == nil)
{
print("finished")
return
}
arr = inputArr
length = arr.count
quicksort(low:0,high:length-1)
}
func quicksort(low: Int, high: Int)
{
var i = low
var j = high
var piv = (low + (high-low))
piv = piv …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 ArrayList 编写选择排序。我的程序要求我创建一个大小为 20 的数组,并用 1 到 1000 之间的随机整数填充它(无需用户输入或硬代码)。输出将需要显示原始的未排序整数列表,并在单独的行上显示排序算法的每次传递。
我尝试创建一个选择排序方法,这就是我陷入困境的地方,因为我不确定如何将代码实现到我的主方法中。
我希望输出结果的示例如下所示(尽管当我尝试执行 20 个整数时,它只显示 5 个整数):
未排序列表: 3 68 298 290 1
通过 1: 1 68 298 290 3
通过 2: 1 3 298 290 68 通过
3: 1 3 68 290 298 通过
4: 1 3 68 290 298
// Used to capture keyboard input
import java.util.*;
// Our class called SelectionSort
public class SelectionSort {
// Create doSelectionSort method
public static int[] doSelectionSort(int[] arr) {
for (int i = …Run Code Online (Sandbox Code Playgroud) Java中是否可以获取当前的排序顺序?
以下是汽车按年份排序的示例:
<p:column headerText="Year" sortBy="#{car.year}">
<h:outputText value="#{car.year}" />
</p:column>
Run Code Online (Sandbox Code Playgroud)
通过扩展LazyDataModel<T>可以重写以下方法:
public List<T> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String,Object> filters) {
throw new UnsupportedOperationException("Lazy loading is not implemented.");
}
Run Code Online (Sandbox Code Playgroud)
但我想知道如何在不使用 LazyDataModel 的情况下做到这一点。
谢谢!
我正在使用 Google Datastore,需要查询它来检索一些实体。这些实体需要按从最新到最旧的顺序排序。我的第一个想法是拥有一个包含时间戳的 date_created 属性。然后我会索引该字段并对该字段进行排序。这种方法的问题是它会导致数据库中出现热点(https://cloud.google.com/datastore/docs/best-practices)。
不要为具有单调递增值的属性(例如 NOW() 时间戳)建立索引。维护此类索引可能会导致出现热点,从而影响具有高读写速率的应用程序的 Cloud Datastore 延迟。
显然,按日期对数据进行排序是对数据库执行的最常见的排序。如果我无法索引时间戳,是否有另一种方法可以实现在没有热点的情况下将我的查询从最新到最旧进行排序?
sorting indexing google-app-engine nosql google-cloud-datastore
我有一个时间戳(长)列表,其中我必须根据时间或添加的列表对列表进行排序。
我已经尝试和搜索过,但它现在可以工作了。
Collections.sort(vehicles, new Comparator<VehicleListModel.Vehicle>() {
@Override
public int compare(VehicleListModel.Vehicle o1, VehicleListModel.Vehicle o2) {
try {
DateFormat format = new SimpleDateFormat("MM-dd-yyyy hh:mm:ss");
return format.parse(o1.getCreated()).compareTo(format.parse(o2.getCreated()));
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
});
customerListAdapter.notifyDataSetChanged();
Run Code Online (Sandbox Code Playgroud)
它不起作用然后我尝试了
这个但它Date(timeStamp)已被弃用
请帮忙
我有一个帖子(新闻)表,但它们可以具有不同的日期属性,其中一些属性可能未设置(0)。我想对所有字段进行 DESC 排序,然后得到一个遵循时间顺序的结果,而不是我在查询中给出的顺序。
title | pubdate | startdate | enddate | closedate |
------------------------------------------------------------
exm1 | 1271887200 | 0 | 0 | 0 |
exm2 | 1291071600 | 0 | 0 | 0 |
exm3 | 0 | 1496102400 | 1496361600 | 0 |
exm4 | 1496620800 | 0 | 0 | 0 |
exm5 | 0 | 1501545600 | 1501891200 | 1496966400 |
exm6 | 0 | 1493856000 | 1496361600 | 0 |
Run Code Online (Sandbox Code Playgroud)
相关字段是“发布日期”、“开始日期”或“关闭日期”。所以我所做的只是将它们放入我的 ORDER BY 语句中:
SELECT * …Run Code Online (Sandbox Code Playgroud) 以下是我的原始数组:
{
FilePath = "Selfcare.jpg";
ModuleId = 18;
ModuleName = "Infant N Toddler";
SkillLevelID = 59;
SkillLevelName = "Self Care";
},
{
FilePath = "Preschool.jpg";
ModuleId = 18;
ModuleName = "Infant N Toddler";
SkillLevelID = 60;
SkillLevelName = "Preschool Readiness";
},
{
FilePath = "Learning_Primary.jpg";
ModuleId = 22;
ModuleName = Cognitive;
SkillLevelID = 71;
SkillLevelName = "Learning Readiness - Primary Skills";
},
{
FilePath = "PreAcad_Primary.jpg";
ModuleId = 22;
ModuleName = Cognitive;
SkillLevelID = 72;
SkillLevelName = "Pre-Academics: Primary Skills"; …Run Code Online (Sandbox Code Playgroud)