Javascript Sorting

Not too long ago, I was asked, as a theoretical question, how I might take a string of random alphabetical characters and sort them in order using Javascript. Thus, given NFARAKDMSLSJDQKJKCXLK, find a simple way to turn it into AACDDFJJKKKKLLMNQRSSX. It’s a pretty basic question for anyone who uses Javascript and I immediately thought of splitting and joining the string as an array (a quick and easy way to tokenize the string). However, for some reason, I completely forgot about the Array.sort() function that’s pretty much a universal across languages.

Instead, the first thing that came to mind was the last time I had to sort a collection and started thinking of Java sorting. In the work I’ve done, such sorting usually requires the use of comparators because the default sort order is often not the one that’s desired. As a case in point, we have a set of JSP pages for rendering content including archive and list pages that display in posting date or sequence order derived from the content. For example, here’s some code to sort by a custom “sequence” attribute using the Vignette Content Management API (this particular snippet is the work of the inimitable Yong Chen):

Comparator aComparator = new Comparator(){
	public int compare(Object obj1, Object obj2) {
		int result=0;
		try {
			Integer s1 = (Integer) ((ContentInstance) obj1).getRelations(CT_RELATION_HEALTH_PLAN)[0].getAttribute("sequence").getValue();
			Integer s2 = (Integer) ((ContentInstance) obj2).getRelations(CT_RELATION_HEALTH_PLAN)[0].getAttribute("sequence").getValue();
			result=s1.compareTo(s2);
		} catch (com.vignette.as.client.exception.ApplicationException e) {
		}
		return result;
	}
};

For debugging, I might break it out a little differently to avoid the multiple casts in a single line. I think the Vignette API is fairly robust, but I would still split the ContentInstance cast apart from the Java native Integer cast in this case. But that might just be me.

In any event, while Javascript can also use comparators without much loss of speed, for a task like the one delineated initially, a simple sort will do the job and the whole can even be condensed to a single line function like this:

function order(unsortedString) {
	return unsortedString.split("").sort().join("");
}

Depending on the browser, the sort function will be some variation on a quick sort or a merge sort. It depends on whether the browser architects chose a stable algorithm or opted for some other version based on various considerations. Mozilla used to use a quicksort prior to Firefox 3.5, but they opted for a merge sort instead in more recent versions. In the course of reading about sort implementations, I came across notes on timsort, Python’s default sort implementation, that I found particularly intriguing.

As an aside the return value for a comparator that works in Firefox may fail with Webkit (Chrome and Safari) since you need to return -1 instead of zero (and a boolean false will also fail). You can read more about Webkit’s Array.sort() handling.

With Java, it looks like this:

String unsorted = "xzya";
char[] content = unsorted.toCharArray();
java.util.Arrays.sort(content);
String sorted = new String(content);

With Perl, you can do something like:

print (join “”, sort split //,$_)

In bash, this will do the same job:

echo “teststring” | grep -o . | sort -n |tr -d ‘\n’; echo

All in all, not only did I refresh my memory about good ways to sort strings and arrays in Javascript, I also learned some new things about sort algorithms and implementations of those algorithms.

Comments Off

Computer Science Education Week (December 6-12)

Computer Science Education Week As a current computer science major, I’m glad to see the ACM in a joint effort to help organize Computer Science Education Week (CSEdWeek), which started December 7th. The week was chosen specifically in honor of Rear Admiral Dr. Grace Murray Hopper, also known as “Amazing Grace”, or “a little old lady who talks to computers,” who was born on December 9, 1906. She was a computer science pioneer and her work, spanning computer languages, compiler verification, software development concepts, and much more, has served to inspire a generation of computer scientists.

Comments Off

Idioms and Economy of Language

I was talking with a co-worker about my limited facility with Japanese and my interest in improving my facility with that language. During the course of our conversation, she mentioned a fascinating aspect of Japanese for expressing certain idioms or proverbs. That’s were I learned about yoji-jukugo (四字熟語), which are idiomatic expressions made up of four kanji characters. These idioms are written only in kanji only and have no kana between them.

One such example would be the Japanese equivalent of the idiom, “two birds with one stone.” This is rendered as isseki nichou (一石二鳥) (literally, “one stone, two birds”). There are many more yoji-jukugo, many of which have similar versions in English (and many other languages besides).

The Chinese equivalent from which yoji-jukugo originate are called chengyu (成语). The same idiom above has a similar chengyu that translates as, “two birds, one arrow” (yī jiàn shuāng diāo, written 一箭双雕). Another one I like is sān rén chéng hǔ (三人成虎) (literally, “three men make a tiger“), which refers to how a repeated rumor may be erroneously accepted as truth.

There are lots of examples of these idioms including a chengyu of the day. You learn something new every day.

Comments Off

Language Impressions

Today I read an interesting perspective on Dutch language, particularly the rather unique ‘ij’ sound in that language. But more than that, it’s a poetic take on the way language seems to feel. (via Snarkmarket)

“There’s something slightly disturbing about the visual scan of the language (I don’t even know what the term is for that: you know when you see a page, or a sign, written in a language and you have an immediate impression of the content of the text? This works also in your native language: look at a page from, like, Dickens, and you can sort of get the Shudder of the Text, or whatever, anyway, what I mean is that some languages, like French, always seem to bear a melismatic philosophy behind the page; German, an authority, Amharic, a crooked delight…) … with Dutch what I get is a sort of childlike pornography: hoog, sneeuwt, poesje, standplaats. But I’m obsessed with it: there’s nothing better than having an old school diagraph still kicking around like an appendix.”

I like the word melismatic in this context, as though the words on the page were the notes and the meaning of the page was the word or melisma. It’s a new one on me even though I subscribe to A.Word.A.Day and occasionally test my vocabulary at FreeRice.

Comments Off