-1

Is there any function that sort the array in fixed random order

for example:

array={"a","b","c","d","e"}

the random sort list should be such as this

array={"b","d","a","c","e"}

The main idea: In case I re-start my app, the list should be fixed

array={"b","d","a","c","e"}
Ali Abdulaal
  • 175
  • 2
  • 15
  • Have you tried writing any code yourself yet, or are you just asking us to write it for you? (assuming we can decipher what you're trying to achieve) – CertainPerformance Apr 15 '18 at 06:08
  • I'm trying now. still my new sorted list not be fixed and my all tries you can find it in other pages. Of course if I could find any solution then I will write it. – Ali Abdulaal Apr 15 '18 at 06:15
  • Please visit the [help] to see what and [ask]. HINT: Post effort and CODE. Show a working sort and ask how to save that is better than asking us to write all your code – mplungjan Apr 15 '18 at 06:20
  • The ideas that you need to combine are probably: 1) writing your own PRNG (that you can start with a known seed) 2) using an array shuffle algorithm – Ates Goral Apr 15 '18 at 06:21
  • yes, i think that also. I tested so many functions and I think I should write my own function that re-order all elements of list to be fixed later – Ali Abdulaal Apr 15 '18 at 06:29
  • Do you mean that you want a permutation algorithm that generates every possible combination/order of your original array? – Thắng Trần Xuân Apr 15 '18 at 06:45
  • This doesn't make much sense to me. If the ordering should be always the same when you restart the application, what is the role of any random shuffler then? What is the difference between random shuffling, shuffling by constant algorithm and hardcoding preshuffled sequence in that case? – Matus Dubrava Apr 15 '18 at 06:46
  • I want to resort them because the original array has different types of elements for example "cars, people, animals". So my result should be fixed and shuffled such as "car4, car2, people1, animal1, car3, animal3, people3, animal2, car1, people2, etc'.. I don't want to show the result such as "car1, car2, car3, people1, people2, animal1, animal2...." – Ali Abdulaal Apr 15 '18 at 07:19

4 Answers4

0

You can use a hash function to create a pseudo-random ordering. An example is given using this hash function, but you can use any hash function you wish:

const hashCode = str => str.split('').reduce((prevHash, currVal) =>
  (((prevHash << 5) - prevHash) + currVal.charCodeAt(0)) | 0, 0) % 100;

const data = ["a", "a", "c", "b", "bb", "car", "dog", "apple", "aaaaaple"];

const compare = (a, b) => hashCode(a) - hashCode(b);

data.sort(compare);
console.log(data);

This particular implementation will return

[ 'apple', 'bb', 'dog', 'car', 'aaaaaple', 'a', 'a', 'b', 'c' ] every time.

AnilRedshift
  • 7,937
  • 7
  • 35
  • 59
0

If you need random array every time you restart app. Try:

var anyArray = new Array("google.com", "yahoo.com" , "a", "c");
var length = anyArray.length;
var usedNums = new Array(4);
function sort (){
    for(i=0;i<length;i++){
        var newOrder;
        do{
        newOrder = getNewNum();
    }
    while(usedNums[newOrder]);

    usedNums[newOrder] = anyArray[i];
    }
    alert(usedNums);


}

function getNewNum(){
    return Math.floor(Math.random()*length);
}
window.onload= sort;
Piotr Mirosz
  • 846
  • 9
  • 20
0

Here is my solution as I want, its very simple :)

shuffle() {
    for(int i=0; i<this.tempList.length/2; i++){
        var l1=[]; var l2=[];
        for(int j=0; j<this.tempList.length; j++){
            if(j%2==1){l1.push(this.tempList[j]);}else{l2.push(this.tempList[j]);}
        }
        this.tempList=l1.concat(l2);
    }
};
Ali Abdulaal
  • 175
  • 2
  • 15
0

Find any pseudorandom generator that accepts seed, and use the same seed each time to get the same number sequence. For example Seeding the random number generator in Javascript

var seed = 1;          // pick the same seed every time
function random() {
    var x = Math.sin(seed++) * 10000;
    return x - Math.floor(x);
}

var array = [ "a", "b", "c", "d", "e" ]

array.sort( random )

console.log( array + '' ) // "e,d,c,b,a"
Slai
  • 22,144
  • 5
  • 45
  • 53