What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Messenger Plus! for Live Messenger » Scripting » [?] Sort array by keys...

[?] Sort array by keys...
Author: Message:
Matti
Elite Member
*****

Avatar
Script Developer and Helper

Posts: 1646
Reputation: 39
31 / Male / Flag
Joined: Apr 2004
RE: [?] Sort array by keys...
In that case, you might be better off with what I suggested in my previous post:
quote:
Originally posted by Matti
a custom sorted hash table class with getter/setter and enumerator methods
This solution would consist of an object (indexed on window ID) and an array (alphabetically sorted by ID) holding the window objects. These indexes can then be stored in the window objects themselves so you can quickly access them in both caches. I liked this idea, so I decided to try this myself. :P

I created a SortedObjectStore class which lets you retrieve and add objects while taking care of the sorting automatically. It exposes getter/enumeration methods (Get, GetBySortIdx and GetCount) and setter methods (Add, ChangeId, Remove and RemoveAll).
Here's an example of how to use it:
Javascript code:
Interfaces.Window = new SortedObjectStore;
 
var Window = {};
Window.Id = "MyWindow";
Interface.Window.Add(Window);
// MyWindow is now in the cache
 
var Window = Interface.Window.Get("MyWindow");
// Retrieved MyWindow from the cache
 
Interface.Window.ChangeId(Window, "YourWindow");
// Window is now called YourWindow instead of MyWindow
 
Interface.Window.Remove(Window);
// YourWindow is now removed from the cache

Looping through the sorted cache is done with GetBySortIdx and GetCount:
Javascript code:
for(var i=0, len=Interface.Window.GetCount(); i<len; ++i) {
    var Window = Interface.Window.GetBySortIdx(i);
    // Loop logic goes here
}

The class can also be used for controls and elements, just create a new instance of it:
Javascript code:
var MyWindow = {};
MyWindow.Id = "MyWindow";
MyWindow.Control = new SortedObjectStore;
MyWindow.Element = new SortedObjectStore;

or a bit more classy:
Javascript code:
var Window = function(Id) {
    this.Id = Id;
    this.Control = new SortedObjectStore;
    this.Element = new SortedObjectStore;
    Interface.Window.Add(this);
}
var MyWindow = new Window("MyWindow");

Some notes:
  • You may not directly change the Id and SortIdx properties of the stored objects, since these are used by the class. You may only directly change the Id property when the object is not in the store, for example when creating an object to add to the store.
  • Both sorting and retrieval are case-insensitive, they use the lowercase version of the Id property.
  • When an object is removed from the store, its SortIdx is set to -1.
  • The class is not foolproof. There are no validation checks to make sure that the right variable types are passed in as parameters, it just assumes that you're doing it right. It's up to you to implement some checks when working with user inputted data.
The class is attached and should be ready to use. Even if you don't plan to use the code, you may learn something from it.

.txt File Attachment: SortedObjectStore.js.txt (1.97 KB)
This file has been downloaded 219 time(s).

This post was edited on 07-31-2010 at 01:02 PM by Matti.
Plus! Script Developer | Plus! Beta Tester | Creator of Countdown Live | Co-developer of Screenshot Sender 5

Found my post useful? Rate me!
07-31-2010 12:56 PM
Profile E-Mail PM Web Find Quote Report
« Next Oldest Return to Top Next Newest »

Messages In This Thread
[?] Sort array by keys... - by whiz on 07-23-2010 at 06:30 PM
RE: [?] Sort array by keys... - by roflmao456 on 07-23-2010 at 07:01 PM
RE: [?] Sort array by keys... - by Adeptus on 07-23-2010 at 07:12 PM
RE: [?] Sort array by keys... - by Eljay on 07-23-2010 at 07:13 PM
RE: [?] Sort array by keys... - by whiz on 07-23-2010 at 07:18 PM
RE: [?] Sort array by keys... - by Matti on 07-30-2010 at 07:47 PM
RE: [?] Sort array by keys... - by whiz on 07-31-2010 at 09:46 AM
RE: [?] Sort array by keys... - by Matti on 07-31-2010 at 12:56 PM


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On