简体   繁体   English

降序排列

[英]Sort in descending order

I got a script from GitHub but its random: https://github.com/dcbriccetti/name-picker 我从GitHub获得了一个脚本,但脚本是随机的: https : //github.com/dcbriccetti/name-picker

I tried editing the picker.js at static shuffle(a). 我尝试在静态shuffle(a)上编辑picker.js。

I wan't it in Ascending Order or Descending Order or some option to make it not random, i want set it by myself. 我不希望它按升序或降序或某些选项使其不随机,我想自己设置。

Here the picker.js code: 这是picker.js代码:

class Picker {
    constructor() {
        this.speech = window.speechSynthesis;
        this.utterance = new SpeechSynthesisUtterance();
        this.cardSpace = [];

        $('#start').click(() => {
            const maxCalls = $('#max-calls').val();
            const names = $('#names').val().split('\n');
            this.cardSpace = [];
            names.map(name => name.trim()).filter(name => name.length > 0).forEach(name => {
                for (let i = 0; i < maxCalls; ++i) {
                    this.cardSpace.push(new NameCard(name));
                }
            });
            Picker.shuffle(this.cardSpace);
        });

        $('#pick').click(() => {
            this.cardSpace = this.cardSpace.filter(card => card.state === card.States.NORMAL);
            if (this.cardSpace.length) {
                const chosenIndex = Math.floor(Math.random() * this.cardSpace.length);
                const card = this.cardSpace[chosenIndex];
                card.pick();
                $('#chosen').text(card.name);
                if ($('#speak').prop('checked')) {
                    this.utterance.text = card.name;
                    this.speech.speak(this.utterance);
                }
            }
        });
    }

    static shuffle(a) {
        for (let i = a.length - 1; i > 0; i--) {
            const j = Math.floor(Math.random() * (i + 1));
            [a[i], a[j]] = [a[j], a[i]];
        }
    }
}

If you want to sort ascending or descending, you need to have a comparator function that returns a 1, 0, or -1 based on comparing two names at a time. 如果要升序或降序排序,则需要具有一个比较器函数,该函数根据一次比较两个名称返回1、0或-1。 You then give that to the .sort() method of your array. 然后,将其提供给数组的.sort()方法。 MDN Docs on Sort MDN文件排序

For example, instead of calling Picker.shuffle() in your code, you might consider calling this.cardSpace.sort(comparer) , where comparer looks like this: 例如,您可以考虑调用this.cardSpace.sort(comparer) Picker.shuffle() ,而不是在代码中调用this.cardSpace.sort(comparer) ,其中比较器如下所示:

function comparer(a, b) {
  var nameA = a.toUpperCase(); // ignore upper and lowercase
  var nameB = b.toUpperCase(); // ignore upper and lowercase
  if (nameA < nameB) {
    return -1;
  }
  if (nameA > nameB) {
    return 1;
  }

  // names must be equal
  return 0;
}

That'll sort ascending (so b comes after a). 排序将递增(因此b在a之后)。 If you want to sort descending, you can just multiply the return value by -1 (based on a variable or setting, for example). 如果要对降序进行排序,则可以将返回值乘以-1(例如,基于变量或设置)。

To have it in default order, instead of random, comment out this line: 要使其具有默认顺序(而不是随机顺序),请注释掉以下行:

 Picker.shuffle(this.cardSpace);

To then take fine control, simply take control of the order that items are added to cardSpace (that is what the this.cardSpace.push(new NameCard(name)); line is doing). 要进行更好的控制,只需控制将项目添加到cardSpace的顺序即可(这是this.cardSpace.push(new NameCard(name));行的作用)。

To have it in reverse order, replace that Picker.shuffle line with: 要使其具有相反的顺序,请将该Picker.shuffle行替换为:

 this.cardSpace.reverse()

(See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse ) (请参阅https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM