如何从具有订单号的值创建有序数组?

时间:2016-10-14 10:14:56

标签: javascript arrays string

所以我有一系列字符串,其中包含图像路径和#连接顺序。

他们看起来像这样:

const images = [
  'photo1.jpg,0'
  'photo2.jpg,2'
  'photo3.jpg,1'
]

所以正确的顺序应该是:photo1,photo3,photo2。

我需要做的是将其处理为只有路径值的正确排序的数组。所以最终我需要:

const orderedImages = [
  'photo1.jpg'
  'photo3.jpg'
  'photo2.jpg'
]

这样做的最佳方式是什么?

5 个答案:

答案 0 :(得分:2)

您可以拆分值并分配给给定的索引。



#include <stdio.h>

#if LINUX
#include <sys/time.h>
#include <sys/resource.h>
double get_time()
{
    struct timeval t;
    struct timezone tzp;
    gettimeofday(&t, &tzp);
    return t.tv_sec + t.tv_usec*1e-6;
}
#else
#include <windows.h>
double get_time()
{
    LARGE_INTEGER t, f;
    QueryPerformanceCounter(&t);
    QueryPerformanceFrequency(&f);
    return (double)t.QuadPart / (double)f.QuadPart * 1000.0;
}
#endif

#define NUM_ITERATIONS (1000 * 1000 * 1000)

// using a macro to avoid function call overhead
#define Benchmark(accumulator, name, operation) { \
    double startTime = get_time(); \
    double dummySum = 0.0, elapsed; \
    int x; \
    for (x = 0; x < NUM_ITERATIONS; x++) { \
        if (operation) dummySum += x; \
    } \
    elapsed = get_time() - startTime; \
    accumulator += elapsed; \
    if (dummySum > 2000) \
        printf("[Test: %-12s] %0.2f ms\r\n", name, elapsed); \
}

void DumpAverage(char *test, double totalTime, double reference)
{
    printf("[Test: %-12s] AVERAGE TIME: %0.2f ms (Relative diff.: %+6.3f%%)\r\n",
        test, totalTime, (totalTime - reference) / reference * 100.0);
}

int main(void)
{
    int repeats = 20;
    double runningTimes[3] = { 0 };
    int k;

    for (k = 0; k < repeats; k++) {
        printf("Run %d of %d...\r\n", k + 1, repeats);
        Benchmark(runningTimes[0], "Plain mod 2", (x % 2));
        Benchmark(runningTimes[1], "Bitwise or", (0xFFFFFFFF == (x | 0xFFFFFFFE)));
        Benchmark(runningTimes[2], "Bitwise and", (x & 1));
    }

    {
        double reference = runningTimes[0] / repeats;
        printf("\r\n");
        DumpAverage("Plain mod 2", runningTimes[0] / repeats, reference);
        DumpAverage("Bitwise or", runningTimes[1] / repeats, reference);
        DumpAverage("Bitwise and", runningTimes[2] / repeats, reference);
    }

    getchar();

    return 0;
}
&#13;
&#13;
&#13;

答案 1 :(得分:2)

您可以使用array.sort对其进行排序,并array.map删除订单值。

const images = [
  'photo1.jpg,0',
  'photo2.jpg,2',
  'photo3.jpg,1'
]
var output = images.sort(function(a,b){
  return +a.split(',')[1] - +b.split(',')[1]
}).map(function(item){
  return item.split(',')[0]
});

console.log(output)

答案 2 :(得分:0)

首先,在 Array#sort 的帮助下对元素进行排序,然后使用 Array#map 方法更新元素。

&#13;
&#13;
var images = [
    'photo1.jpg,0',
    'photo2.jpg,2',
    'photo3.jpg,1'
  ]
  // sort the element
  .sort(function(a, b) {
    // split the element and calculate 
    // return value based on that
    return a.split(',')[1] - b.split(',')[1];
    // iterate and generate new array with updated value
  }).map(function(v) {
    // generate array elemnt by removing the number part
    return v.split(',')[0]
  })


console.log(images)
&#13;
&#13;
&#13;

ES6 arrow function

&#13;
&#13;
var images = [
  'photo1.jpg,0',
  'photo2.jpg,2',
  'photo3.jpg,1'
].sort((a, b) => a.split(',')[1] - b.split(',')[1]).map(v => v.split(',')[0])


console.log(images)
&#13;
&#13;
&#13;

答案 3 :(得分:0)

尝试这种方法

var images = [
  'photo1.jpg,0',
  'photo2.jpg,2',
  'photo3.jpg,1'
];
images.sort(function(a,b){
   return Number(a.split(",")[1]) - Number(b.split(",")[1])
});
var orderedImages = images.map(function(item){
  return item.split(",")[0];
});
console.log(orderedImages);

答案 4 :(得分:0)

使用Lodash并简单地写:

var orderedImages = _(images)
  .sortBy(function(element) { return element.split[','][1] ; })
  .map(function(element) { return element.split[','][0]; })
  .value();