如何排序数组的顺序?

时间:2019-06-18 17:47:20

标签: javascript jquery

我有一个数组中的文章列表。文章中有不同类型的文章,即“ Heros”,“新闻”和“公告”,它们按日期排序。我希望将前2篇“英雄”文章移到数组的开头。任何帮助将非常感激。我为此一直在绞尽脑汁。

self.loadArticles(url, false, isNewProcess, function(news) {
            self.loadArticles(url, true, isNewProcess, function(announcements) {
                var tempArticles = news.concat(announcements),
                    articles = [],
                    heroes = [];

                tempArticles.sort(function(a, b) {
                    if (a.NewsPublishedDate > b.NewsPublishedDate) return -1;
                    if (b.NewsPublishedDate > a.NewsPublishedDate) return 1;

                    return 0;
                });

                var hero = null;
                for (var i = 0; i < tempArticles.length; i++) {
                    article = self.convertToArticle(tempArticles[i]);

                    // Extract hero if marked as hero
                    if (article.hero) {
                        heroes.push(article);
                    } else {
                        articles.push(article);
                    }
                }

                self.articles[data.Title] = articles;

                // Add the hero article
                if (heroes.length) {
                    var hero = heroes.shift();
                    $hero = self.generateArticleHTML(hero);
                    $hero.appendTo($targetHero);
                }

                // Combine remaining heroes with articles
                if (heroes.length) {
                    articles = heroes.concat(articles);
                }

                // Create HTML for the first 6 items
                for (var i = 0; i < 6; i++) {
                    var article = articles[i];
                    $article = self.generateArticleHTML(article);
                    $article.appendTo($targetList);
                }

            });
        });

这是我目前得到的

0: {title: "article 1", hero: false, pubDate: "26/04/2019"}
1: {title: "article 2", hero: false, pubDate: "25/04/2019"}
2: {title: "article 3", hero: true, pubDate: "24/04/2019"}
3: {title: "article 4", hero: false, pubDate: "23/04/2019"}
4: {title: "article 5", hero: true, pubDate: "22/04/2019"}
5: {title: "article 6", hero: false, pubDate: "21/04/2019"}
6: {title: "article 7", hero: true, pubDate: "20/04/2019"}

我希望出来

2: {title: "article 3", hero: true, pubDate: "24/04/2019"}
4: {title: "article 5", hero: true, pubDate: "22/04/2019"}
0: {title: "article 1", hero: false, pubDate: "26/04/2019"}
1: {title: "article 2", hero: false, pubDate: "25/04/2019"}
3: {title: "article 4", hero: false, pubDate: "23/04/2019"}
5: {title: "article 6", hero: false, pubDate: "21/04/2019"}
6: {title: "article 7", hero: true, pubDate: "20/04/2019"}

再次感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

好像您要明确地将某些索引移到最前面。 splice删除索引处的元素,而unshift则将元素插入到最前面。

let moveFront = (arr, index) => arr.unshift(...arr.splice(index, 1));

let arr = [
	{title: "article 1", hero: false, pubDate: "26/04/2019"},
	{title: "article 2", hero: false, pubDate: "25/04/2019"},
	{title: "article 3", hero: true, pubDate: "24/04/2019"},
	{title: "article 4", hero: false, pubDate: "23/04/2019"},
	{title: "article 5", hero: true, pubDate: "22/04/2019"},
	{title: "article 6", hero: false, pubDate: "21/04/2019"},
	{title: "article 7", hero: true, pubDate: "20/04/2019"},
];

moveFront(arr, 4); // 'article 5;
moveFront(arr, 3); // 'article 3;

console.log(arr);

答案 1 :(得分:0)

对原始数组进行排序,提取任意数量的英雄(如果需要两个以上的英雄,将其循环播放),然后以相反的顺序重新插入前面。

function sortArr(arr){
    let GetDate = (pubDate)=>{ //extract the real date from pubDate
        let date = pubDate.split("/");
        let day = date[0];
        let month = date[1];
        let year = date[2];
        return new Date(year,month,day);
    }

    //reverse it if needed
    arr.sort((x,y) => GetDate(x.pubDate) - GetDate(y.pubDate)); //first sort everything by pubDate. Do NOT use string comparisons, must extract the actual date

    let arrCopy = arr.slice();

    //find and remove the first 2 heros
    let firstHeroIndx = arrCopy.findIndex(x=>x.hero);
    let secondHeroIndx = -1;

    if(firstHeroIndx >=0){
        arrCopy.splice(firstHeroIndx,1);

        secondHeroIndx = arrCopy.findIndex(x=>x.hero);
        if(secondHeroIndx >=0){
            arrCopy.splice(secondHeroIndx,1);
            secondHeroIndx ++;// must add 1 since the firstHero was removed before it
        }
    }

    //reinsert the heros
    if(secondHeroIndx >=0){
        arrCopy.unshift(arr[secondHeroIndx]);
    }
    if(firstHeroIndx >=0){
        arrCopy.unshift(arr[firstHeroIndx]);
    }

    return arrCopy;
}


let arr = [
    {title: "article 1", hero: false, pubDate: "26/04/2019"},
    {title: "article 2", hero: false, pubDate: "25/04/2019"},
    {title: "article 3", hero: true, pubDate: "24/04/2019"},
    {title: "article 4", hero: false, pubDate: "23/04/2019"},
    {title: "article 5", hero: true, pubDate: "22/04/2019"},
    {title: "article 6", hero: false, pubDate: "21/04/2019"},
    {title: "article 7", hero: true, pubDate: "20/04/2019"},
];

sortArr(arr);

// will create the following array
// [
//     {
//       "title": "article 7",
//       "hero": true,
//       "pubDate": "20/04/2019"
//     },
//     {
//       "title": "article 5",
//       "hero": true,
//       "pubDate": "22/04/2019"
//     },
//     {
//       "title": "article 6",
//       "hero": false,
//       "pubDate": "21/04/2019"
//     },
//     {
//       "title": "article 4",
//       "hero": false,
//       "pubDate": "23/04/2019"
//     },
//     {
//       "title": "article 3",
//       "hero": true,
//       "pubDate": "24/04/2019"
//     },
//     {
//       "title": "article 2",
//       "hero": false,
//       "pubDate": "25/04/2019"
//     },
//     {
//       "title": "article 1",
//       "hero": false,
//       "pubDate": "26/04/2019"
//     }
//   ]