我有一个对象数组。每个对象都有一个“日期类型”;上午下午晚上。由于异步差异,每次我得到列表时它们都是随机排列的...但我需要按照以下顺序显示它们:早上,下午,晚上。
如果我使用array.sort(),它们会按顺序出现:下午,晚上,早上。因为它按字母顺序排序吗?
所以......我需要一个比较功能。但我无法弄清楚如何制作它,因为我发现的所有例子只有两个输入。排序(a,b)......
INFO: From Compiling tensorflow/core/kernels/histogram_op_gpu.cu.cc:
./tensorflow/core/lib/core/status.h(32): warning: attribute "warn_unused_result" does not apply here
external/protobuf_archive/src/google/protobuf/arena.h(719): error: more than one instance of overloaded function "google::protobuf::Arena::CreateMessageInternal" matches the argument list:
function template "T *google::protobuf::Arena::CreateMessageInternal<T>(google::protobuf::Arena *)"
function template "T *google::protobuf::Arena::CreateMessageInternal<T,Args...>(Args &&...)"
argument types are: (google::protobuf::Arena *)
detected during:
instantiation of "Msg *google::protobuf::Arena::CreateMaybeMessage<Msg>(google::protobuf::Arena *, google::protobuf::internal::true_type) [with Msg=tensorflow::TensorShapeProto_Dim]"
(729): here
instantiation of "T *google::protobuf::Arena::CreateMaybeMessage<T>(google::protobuf::Arena *) [with T=tensorflow::TensorShapeProto_Dim]"
external/protobuf_archive/src/google/protobuf/repeated_field.h(648): here
instantiation of "GenericType *google::protobuf::internal::GenericTypeHandler<GenericType>::New(google::protobuf::Arena *) [with GenericType=tensorflow::TensorShapeProto_Dim]"
external/protobuf_archive/src/google/protobuf/repeated_field.h(675): here
instantiation of "GenericType *google::protobuf::internal::GenericTypeHandler<GenericType>::NewFromPrototype(const GenericType *, google::protobuf::Arena *) [with GenericType=tensorflow::TensorShapeProto_Dim]"
external/protobuf_archive/src/google/protobuf/repeated_field.h(1554): here
instantiation of "TypeHandler::Type *google::protobuf::internal::RepeatedPtrFieldBase::Add<TypeHandler>(TypeHandler::Type *) [with TypeHandler=google::protobuf::RepeatedPtrField<tensorflow::TensorShapeProto_Dim>::TypeHandler]"
external/protobuf_archive/src/google/protobuf/repeated_field.h(2001): here
instantiation of "Element *google::protobuf::RepeatedPtrField<Element>::Add() [with Element=tensorflow::TensorShapeProto_Dim]"
bazel-out/local_darwin-opt/genfiles/tensorflow/core/framework/tensor_shape.pb.h(471): here
....
我希望这是可能的,它可能是小菜一碟......其他明智的我已经想到了这样的解决方案
day.shifts = randomOrder;
day.shifts.sort((a, b, c) => {
//some how compare all 3
}
如果可行,我更喜欢比较方法。或者至少比分离数组和连接方法的分裂更“漂亮”......
列表中的对象如下所示:
day.shifts = randomOrder;
const morning = [];
const afternoon = [];
const evening = [];
day.shifts.forEach(shift => {
if(shift.type === 'morning'){
morning.push(shift)
}
if(shift.type === 'afternoon'){
afternoone.push(shift)
}
if(shift.type === 'evening'){
evening.push(shift)
}
}
day.shifts = morning.concat(afternoon).concat(evening);
答案 0 :(得分:2)
你可以这样对它们进行排序:
const order = ["morning", "afternoon", "evening"];
day.shifts.sort((a, b) => order.indexOf(a.type) - order.indexOf(b.type));
这基本上是在排序数组中排在第一位后对排序进行排序。
答案 1 :(得分:1)
您可以按类型对订单进行排序。
var types = { morning: 1, afternoon: 2, evening: 3 };
day.shifts.sort((a, b) => types[a.type] - types[b.type]);