Wednesday, May 5, 2010

JavaScript Sorting

Many of you are aware of the built-in sort function for sorting JavaScript arrays. But you might not realize the true power of this function. This tip will talk about how this built-in function can be used to sort multi-dimensional arrays.

If you have a one-dimensional JavaScript array, then calling the built-in sort method on the array will sort the array in ascending order. But what about the case where you have an array of first names, an array of last names, and an array of ages? You may want to sort the array of first names, but keep the corresponding positions in the last names and ages. This is done by setting up a multi-dimensional array and sorting the proper "column" of the multi-dimensional array.

First, setting up a multi-dimensional array is pretty easy. If you know the values ahead of time, they can be specified in your javascript code:

var myData = new Array();
myData[0] = {FirstName:"John", LastName:"Doe", Age:40};
myData[1] = {FirstName:"Fred", LastName:"Smith", Age:41};
and so on.

If you don't know the values ahead of time, then the easiest thing to do is to build a "constructor" function:

var myData = new Array();
function person(firstName, lastName, age) {
this.FirstName = firstName;
this.LastName = lastName;
this.Age = parseInt(parseFloat(age));
}

Then, to add a new entry to the array, it's just a matter of creating a new person object:

people[people.length++] = new person(form.FirstName.value, form.LastName.value, form.Age.value);

(The example has the values coming from user input on a form).

To sort the array, we need to tell JavaScript how to sort. This comes from a function that returns -1, 0, or +1 as a value. -1 will put the first value before the second in the sorted array, +1 will put the second value before the first, and 0 means the values are equal. For example, if we want to sort by the first names, here is a function that can be used:

function sortByFirstName(a, b) {
var x = a.FirstName.toLowerCase();
var y = b.FirstName.toLowerCase();
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}

Two values are passed to this function. The values are array objects. If the first name part of the first object is alphabetically less than the second, -1 is returned. If the first name part of the first object is alphabetically greater than the second, +1 is returned. Otherwise, the first name values must be identical and 0 is returned.

But where does this function come in to play? There's an optional parameter to the built-in sort method that can be used. If you pass in a function name, that function name is used to compare elements in the array for sorting purposes. So, using this javascript call:

people.sort(sortByFirstName);

the array will be sorted in ascending order by the first name part of each array object. Similar functions and calls can be created to sort by the last name or by the age.

Note that JavaScript sorts in ascending order by default. But our function returns how things should be ordered. If we changed +1 to -1 in the function (and vice-versa) then the array would sort the first names in descending order. This is how you can sort by descending order.

JavaScript Sorting

Friday, April 30, 2010

day 43 and I write poetry

today (in particular)
I miss speaking the language
that we created,
learned,
and in which only we are fluent,
the one comprised of sound effects, hand effects,
the one reserved for perfectly communicating
thoughts that are like
unfinished sentences, tongue clicks, stuffed animal psyche,
a language like a try/catch block,
collecting thoughts mildly ignorant, conjecture and insecure,
unspeakably offensive and intoxicating,
(but today, its)
a language that feels like
a short-term memory, like
being stuck
(without you)
in a jar of peanut butter
(with which to speak it)

Wednesday, March 17, 2010

how to query duplicate records in SQL

SELECT *
FROM Person.Address A
INNER JOIN (SELECT AddressLine1, City
FROM Person.[Address]
GROUP BY AddressLine1, City
HAVING COUNT(*) > 1) AS A1
ON A.AddressLine1 = A1.AddressLine1
AND A.City = A1.City
ORDER BY A.AddressLine1, A.City, A.AddressID

Monday, March 1, 2010

how to get a list of tables in a database using C#

Unsurprisingly, if a table doesn't exist in a database, you can't use a select query to determine whether it exists. But using GetSchema() with no parameters, you can obtain a schema table, which is essentially a list of tables you can specify to get more specific schema information. In this case, I wanted to get a list of tables in the database. After transforming the results into a list of strings, I could then safely search the list to determine whether it exists.


SqlConnection con =
new SqlConnection("connection_string");
con.Open();
DataTable tbl =
con.GetSchema("Tables");


Schema and metadata retrieval using ADO.NET

this also works, depending on whether you need these extra parameters, but its a big uglier:


DataTable tables =
con.GetOleDbSchemaTable
(OleDbSchemaGuid.Tables,
new object[]{null,null,null,"TABLE"});


Get all table names

Wednesday, February 24, 2010

fixed my "Restore failed...The file cannot be overwritten. It is being used by database..." error

The "Database -> Restore Database... -> Options pane -> Restore the database files as" file names in the "Restore As" fields were not "Test1Copy1" as entered into the "General pane -> Destination for restore -> To database" field as the database restore destination. The file names were in fact "Test1," the same name of the "General pane -> Destination for restore -> From database" field. Basically, the file names should be Test1Copy1, but for some reason default to Test1.

The... error is resolved by clicking into the "Options pane -> Restore the database files as" file names in the Restore As fields and manually changing the file names...

FROM: Test1.mdf
TO: Test1Copy1.mdf

and

FROM: Test1_log.ldf
TO: Test1Copy1_log.ldf

This could be a SQL Server Management Studio bug, but it is just as likely a misunderstanding by a user who is just realizing he not only doesn't know his @ss from a hole in the ground; he didn't even know there was a hole in the ground ;-)

<%= Clinton Gallagher

Slightly scathing at the very end, Clinton Gallagher, very sneaky, but I do appreciate the apparent concern for my virgin eyes and for the bug fix :-P

http://www.sqlnewsgroups.net/group/microsoft.public.sqlserver.server/topic12008.aspx

Thursday, February 18, 2010

fixed my " TypeLoadException: Could not load type from string value" error

3.2.4.1. Object creation of generic types via constructor invocation
The following examples shows the definition of simple generic types and how they can be created in Spring's XML based configuration file.
namespace GenericsPlay
{
public class FilterableList<T>
{
private List<T> list;
private String name;

public List<T> Contents
{
get { return list; }
set { list = value; }
}

public String Name
{
get { return name; }
set { name = value; }
}

public List<T> ApplyFilter(string filterExpression)
{
/// should really apply filter to list ;)
return new List<T>();
}
}
}

The XML configuration to create and configure this object is shown below

<object id="myFilteredIntList" type="GenericsPlay.FilterableList&lt;int&gt;, GenericsPlay">
<property name="Name" value="My Integer List"/>
</object>

Chapter 3. Objects, Object Factories, and Application Contexts

Monday, February 8, 2010

MJ
3:41 PM
I am actually surprised
3:42 PM
because I realized over the weekend
3:42 PM
it would be really nice to live with you