hshackers / docs

Documentation for HS Hackers
hshackers.org
30 stars 56 forks source link

Alphabetized list by first name #55

Closed raphaelrk closed 10 years ago

raphaelrk commented 10 years ago

Here's my javascript automation code for it. Run the code while in your browser's console on the edit page for the csv. It'll print out what should be pasted in the file. It changes the innerHTML because I thought it'd keep one from needing to copy and paste, since github's editor would see you made a change, but I was wrong, it didn't detect a change. There's also more code I wrote for this that edits all the divs in the editor to be alphabetical (this is a visible page change), but GitHub's editor still didn't count it as a file change. Because I was trying this I had to roll my own sorting method since the divs were in a nodelist, not an array.

// the edit area
var textarea = document.getElementById('blob_contents');

// the csv lines
var itemsToSort = textarea.innerHTML.split('\n');

// bubble sorts the elements in itemArr by their lowercase string values
var bubbleSort = function(itemArr, startIndex) {
 var somethingChanged = true;
 while(somethingChanged === true) {
  somethingChanged = false;

  for(var i = startIndex; i < itemArr.length - 1; i++) {
   // lowercase innerHTML to comper alphabetical order
   var text1 = itemArr[i].toLowerCase();
   var text2 = itemArr[i+1].toLowerCase();

   if(text2 < text1) {
    somethingChanged = true;

    var temp = itemArr[i];
    itemArr[i] = itemArr[i+1];
    itemArr[i+1] = temp;
   }
  }
 }
};

// Call bubblesort on items. Having 1 as startindex removes 'First,Last,Website,GitHub,Twitter,Facebook' from what'll be sorted
bubbleSort(itemsToSort, 1);

// finally, replace textarea's innerHTML with the new values
textarea.innerHTML = "";
for(var i = 0; i < itemsToSort.length; i++) {
 textarea.innerHTML += itemsToSort[i] + '\n';
}

// And since that doesn't "change" anything, print out the textarea's innerHTML to copy and paste into the editor
console.log(textarea.innerHTML);
raphaelrk commented 10 years ago

That code's been bugging me. It was really bloated from failed attempts, and had a bubble sort in it. Here's a nicer version: (Note: 'moves First,Last,Website,GitHub,Twitter,Facebook')

var textarea = document.getElementById('blob_contents'); // the edit area
var itemsToSort = textarea.innerHTML.split('\n'); // the csv lines
itemsToSort.sort(); // sort the items
var printout = "" // string to populate
for(var i = 0; i < itemsToSort.length; i++) printout += itemsToSort[i] + '\n';
console.log(printout); // print out alphabetized doc
zachlatta commented 10 years ago

:thumbsup: