.sort返回一个映射只包含一个元素

时间:2022-01-02 17:11:00

I have a map contains 10 entries as shown in the 1st 10 lines in the "logs" section below. the map is populated with values as follows:

我有一个地图包含10个条目,如下面的“日志”部分的第10行。地图上的值如下所示:

const featuresMap =  {};
for (const feature of features) {
  featuresMap[feature.getName()] = feature.getPriority();
  logger.info(': ++++++featuresMap ', featuresMap, '++++++');
}
 const sortedFeaturesPerPriority = this.sortFeaturesPerPriority(featuresPrioritiesTags);
logger.info('[delayOrchestration]: featuresPrioritiesTagsSORTED : ', sortedFeaturesPerPriority);

then i tried to sort the map according to its values, using the code shown in section "code_sorting". The problem is that the sorting method in section "code_sorting" which is posted below is just return the last item and NOT a map contains all the items sorted.

然后,我尝试按其值对映射进行排序,使用“code_sort”一节中所示的代码。问题是,下面发布的“code_sorts”中的排序方法只返回最后一个条目,而不是一个map包含所有已排序的条目。

please let me know how how to get a map conatina all the entries sorted according to the values of the map.

请让我知道如何得到一个地图conatina所有的条目根据地图的值排序。

code_sorting:

code_sorting:

sortFeaturesPerPriority(map) {
const mapSorted = Object
  .keys(map)
  .sort((a, b) => map[a] - map[b])
  .reduce((a, c) => (a[c] = map[c], a), {});
logger.info('[sortFeaturesPerPriority] mapSorted: ', mapSorted);
return mapSorted;
}

logs:

日志:

 INFO: [ProgramFactory] : ++++++featuresMap [["TEST",1]]++++++
 INFO: [ProgramFactory] : ++++++featuresMap [["TEST",1]]++++++
 INFO: [ProgramFactory] : ++++++featuresMap [["TEST",1]]++++++
 INFO: [ProgramFactory] : ++++++featuresMap [["TEST",5]]++++++
 INFO: [ProgramFactory] : ++++++featuresMap [["TEST",5]]++++++
 INFO: [ProgramFactory] : ++++++featuresMap [["TEST",2]]++++++
 INFO: [ProgramFactory] : ++++++featuresMap [["TEST",2]]++++++
 INFO: [ProgramFactory] : ++++++featuresMap [["TEST",2]]++++++
 INFO: [ProgramFactory] : ++++++featuresMap [["TEST",2]]++++++
 INFO: [ProgramFactory] : ++++++featuresMap [["TEST",6]]++++++

 //logs from the sorting method
[sortFeaturesPerPriority] mapSorted: {"TEST":6}
[delayOrchestration]: featuresPrioritiesTagsSORTED : {"TEST":6}

3 个解决方案

#1


2  

it looks like every feature is returning the same name of 'TEST', so you're just overwriting the same property each time.

看起来每个特性都返回相同的“TEST”名称,因此每次都覆盖相同的属性。

#2


0  

There are several problems with that code. The fact that you are getting only the last {"TEST":6} is because you are creating properties of that object with all of them having the same name so that it will get overwritten each time, producing object with only one property. You can't have object properties like this:

这段代码有几个问题。您只获得最后一个{"TEST":6}的事实是因为您正在创建对象的属性,其中所有的属性都具有相同的名称,以便每次都被覆盖,从而产生只有一个属性的对象。你不能有这样的对象属性:

const obj = {
    test: 1,
    test: 2
}

as the second test overwrites the first one.

当第二个测试覆盖第一个测试时。

Another issue is that even if you changed names of those properties, you are sorting them into a new object, where you have no guarantee that they will remain sorted as the ordering of the properties of an object does not need to follow the order in which they were created.

另一个问题是,即使你改变这些属性的名字,你是排序成一个新对象,在你没有保证他们仍将排序的排序属性的对象不需要跟随他们创建的顺序。

You could change the mapSorted like this and see if it fits your needs.

你可以改变这个地图,看看它是否符合你的需要。

const mapSorted = Object
    .keys(map)
    .sort((a, b) => map[a] - map[b])
    .map(item => {
         return { [item]: map[item] }
    });

#3


0  

If you need a sorted structure, you need to use an array (and not an object). There is several ways to proceed.

如果需要排序结构,则需要使用数组(而不是对象)。有几种方法可以进行。

@Matus Dubrava's answer provides you an array containing sorted elements like {name: priority}.

@Matus Dubrava的答案提供了一个包含排序元素的数组,如{name: priority}。

Another way to proceed is to deal with an array from the beginning:

另一种处理方法是从一开始处理数组:

const featuresMap = [];
for (const feature of features) {
    featuresMap.push({name: feature.getName(), priority: feature.getPriority()});
}
const sortedMap = featuresMap.sort((a, b) => a.priority - b.priority)

Otherwise, if you just need sorted names:

否则,如果您只需要排序的名称:

const sortedNames = Object.keys(map).sort((a, b) => map[a] - map[b]);

#1


2  

it looks like every feature is returning the same name of 'TEST', so you're just overwriting the same property each time.

看起来每个特性都返回相同的“TEST”名称,因此每次都覆盖相同的属性。

#2


0  

There are several problems with that code. The fact that you are getting only the last {"TEST":6} is because you are creating properties of that object with all of them having the same name so that it will get overwritten each time, producing object with only one property. You can't have object properties like this:

这段代码有几个问题。您只获得最后一个{"TEST":6}的事实是因为您正在创建对象的属性,其中所有的属性都具有相同的名称,以便每次都被覆盖,从而产生只有一个属性的对象。你不能有这样的对象属性:

const obj = {
    test: 1,
    test: 2
}

as the second test overwrites the first one.

当第二个测试覆盖第一个测试时。

Another issue is that even if you changed names of those properties, you are sorting them into a new object, where you have no guarantee that they will remain sorted as the ordering of the properties of an object does not need to follow the order in which they were created.

另一个问题是,即使你改变这些属性的名字,你是排序成一个新对象,在你没有保证他们仍将排序的排序属性的对象不需要跟随他们创建的顺序。

You could change the mapSorted like this and see if it fits your needs.

你可以改变这个地图,看看它是否符合你的需要。

const mapSorted = Object
    .keys(map)
    .sort((a, b) => map[a] - map[b])
    .map(item => {
         return { [item]: map[item] }
    });

#3


0  

If you need a sorted structure, you need to use an array (and not an object). There is several ways to proceed.

如果需要排序结构,则需要使用数组(而不是对象)。有几种方法可以进行。

@Matus Dubrava's answer provides you an array containing sorted elements like {name: priority}.

@Matus Dubrava的答案提供了一个包含排序元素的数组,如{name: priority}。

Another way to proceed is to deal with an array from the beginning:

另一种处理方法是从一开始处理数组:

const featuresMap = [];
for (const feature of features) {
    featuresMap.push({name: feature.getName(), priority: feature.getPriority()});
}
const sortedMap = featuresMap.sort((a, b) => a.priority - b.priority)

Otherwise, if you just need sorted names:

否则,如果您只需要排序的名称:

const sortedNames = Object.keys(map).sort((a, b) => map[a] - map[b]);