Greasy Fork is available in English.

Discussions » Development

re-sort HTML table

ihf
§
Posted: 01 Agustus 2016

re-sort HTML table

I would like to know if the following is possible and, if so, roughly how difficult it will be to do. If trivial, perhaps someone could offer some code.
Here is a typical webpage (http://streeteasy.com/building/800-west-end-avenue-new_york#tab_building_detail=2).
I am seeking to re-sort the Units table so that the order is based on the last character of the Unit followed by the remainder of the string. For example, on this page the 1st 3 Units are "8C", "5D", and "12C". I would like to reorder this table to be "8C", "12C", "5D". The page already has the ability to re-sort in alpha order but I would like to alter the Units order from any page of this type from this site.
Thanks.

woxxomMod
§
Posted: 01 Agustus 2016

Maybe you can simply use ActiveTable bookmarklet.

§
Posted: 02 Agustus 2016

So you would need to sort by the last character first then the number that precedes it? I'm not sure you'll find any pre-baked add-on or script for that, you may need to have one custom written.

There are some edge cases, like EF and E/F so this is messier than I thought.

§
Posted: 02 Agustus 2016
Edited: 02 Agustus 2016

Try running this in Firefox's web console, it rewrites the sort keys in that column of the table as follows, and then you can use the built-in sort button instead of writing your own sorting script:

#12C => #C1012
#8C => #C1008

var units=document.querySelectorAll('.activity_unit'); 
for (var i=0; i<units.length; i++) {
  var unum=units[i].getAttribute('data-sort-value').substr(1);
  if (unum.length > 1) {
    var newnum = parseInt(unum);
    if (Number.isNaN(newnum) === false) {
      var textpart = unum.substr(newnum.toString().length);
      newnum = newnum + 1000;
      units[i].setAttribute('data-sort-value', '#' + textpart + newnum);
    }
  }
}

This was about the dozenth thing I tried, so I would say "not trivial."

ihf
§
Posted: 02 Agustus 2016

Thank you! If I paste your code into the console (After doing "allow pasting") and hit enter, I get "undefined".

§
Posted: 02 Agustus 2016

You get undefined because the code isn't meant to display any data in the console. But does the sorting work after that?

ihf
§
Posted: 02 Agustus 2016

At first I thought not, but I just tried again and it worked perfectly. Thanks very much!

ihf
§
Posted: 02 Agustus 2016

This is odd. I tried it again with another page from that site (http://streeteasy.com/building/473-west-end-avenue-new_york#tab_building_detail=2) and it failed to sort correctly.

ihf
§
Posted: 02 Agustus 2016

I did it again and it worked on that page. Not sure what I am doing that's different but it does indeed do the sort.

Post reply

Sign in to post a reply.