Friday, September 23, 2011

Javascript: array sort descending


//Sort alphabetically and ascending:
var myarray=["Bob", "Bully", "Amy"]
myarray.sort() //Array now becomes ["Amy", "Bob", "Bully"]



//Sort alphabetically and descending:
var myarray=["Bob", "Bully", "Amy"]
myarray.sort()
myarray.reverse() //Array now becomes ["Bully", "Bob", "Amy"]

Source:
http://www.javascriptkit.com/javatutors/arraysort.shtml

Javascript: remove item from array

DON’T
Unfortunately the most obvioous solution leaves a “hole” in the array :-(.
>>> var list = [4,5,6];
>>> delete list[1];
true
>>> list
[4, undefined, 6]
Sad, but understandable.

DO
So do it right and use splice().
>>> var list = [4,5,6];
>>> list.splice(1, 1); // Remove one element, returns the removed ones.
[5]
>>> list
[4, 6]


Useful DO
Actually, what I mostly need is: remove a certain value from an array.
I.e. I have some list of visible IDs and I want to remove one because it’s not visible anymore. So just extend the above example a bit.
>>> var visibleIds = [4,5,6];
>>> var idx = visibleIds.indexOf(5); // Find the index
>>> if(idx!=-1) visibleIds.splice(idx, 1); // Remove it if really found!
[5]
>>> visibleIds
[4, 6]


Source:
http://wolfram.kriesing.de/blog/index.php/2008/javascript-remove-element-from-array
Resource:
http://www.w3schools.com/jsref/jsref_splice.asp
http://stackoverflow.com/questions/5767325/remove-specific-element-from-a-javascript-array

Wednesday, September 21, 2011

MS SQL 2005: varchar to datetime - La conversión del tipo de datos varchar en datetime produjo un valor fuera de intervalo error.

I was trying to execute this:

update d_entrada set fecha_radicado = cast('2011-09-21 20:41:06.000' as datetime)  where id = '123456';


and I got this error

La conversión del tipo de datos varchar en datetime produjo un valor fuera de intervalo error.

I put the datetime this way ('2011-09-21 20:41:06.000') because when I execute

select fecha_radicado from d_entrada where id = '123456';

I get

2011-09-21 20:41:06.000

But to make the update query work propertly I had to switch 09 and 21 date values:

update d_entrada set fecha_radicado = cast('2011-21-09 20:41:06.000' as datetime)  where id = '123456';


then I got:



(1 row(s) affected)


Google Chrome: start incognito mode by default

1. Right click on google chrome shorcut
2. Select "properties" option
3. At the end of the target url add a "-incongnito" i.e :

C:\Users\John\AppData\Local\Google\Chrome\Application\chrome.exe -incognito

Note, sometimes it works with -incongnito or --incongnito depending the Operating System.

Source:
http://www.makeuseof.com/tag/how-to-start-google-chrome-in-incognito-mode-by-default/

Sunday, September 18, 2011

ExtJS (2.2.1): Making ComboBox Filter work from first click.

HAdd the following properties to the combobox that you need to filter:

triggerAction: 'all',
lastQuery: '',

Source:
http://www.sencha.com/forum/showthread.php?26909-linked-combobox-onload-filter&langid=4

Friday, September 9, 2011

ExtJS (2.2.1): xtype button listeners

{
    xtype:'button'
    ,text:'add'
    ,listeners:{                                        
        'click' : function(){alert("hi!!");
        }                                        
}   
Reference:
http://stackoverflow.com/questions/2372353/extjs-how-can-i-add-event-handlers-to-a-component-in-its-raw-object-form-xtype

ExtJS (2.2.1): Ext.Button absolute position

A Button is not an Ext.BoxComponent, so it cannot be positioned using x and y properties. So the problem can be solved by adding the button to a transparent panel with the same size as the button.


var MyPanel = new Ext.Panel({
            layout:'absolute',
            width:400,
            height:200,
            items:[{
                xtype:'textfield',
                width:150,
                x:0,
                y:0
            },{
                xtype:'combo',
                width:150,
                x:160,
                y:0
            },{
                xtype:'panel',
                y:25,
                x:0,
                items:[
                {
                    xtype:'button',
                    text:'Search'
                }]
            }]
});

Source:
http://www.sencha.com/forum/showthread.php?37012-Ext.Button-absolute-position-help

HTML:Great Canvas Tutorial

Thursday, September 8, 2011

Javascript: string replace

var myString = "thisisastring";
myString.replace("i", "e");

Reference:

Java: Iterate a String


java.text.CharacterIterator;
java.text.StringCharacterIterator;
CharacterIterator it = new StringCharacterIterator("abcd");

// Iterate over the characters in the forward direction
for (char ch=it.first(); ch != CharacterIterator.DONE; ch=it.next()) {
    // Use ch ...
}
Reference:
http://www.exampledepot.com/egs/java.text/StrIter.html

Wednesday, September 7, 2011

Javascript: currency format function


function formatCurrency(num) {
   num = num.toString().replace(/\$|\,/g,'');
   if(isNaN(num))
   num = "0";
   sign = (num == (num = Math.abs(num)));
   num = Math.floor(num*100+0.50000000001);
   cents = num%100;
   num = Math.floor(num/100).toString();
   if(cents<10)
   cents = "0" + cents;
   for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
   num = num.substring(0,num.length-(4*i+3))+','+
   num.substring(num.length-(4*i+3));
   return (((sign)?'':'-') + '$' + num + '.' + cents);
}


Source:
http://javascript.internet.com/forms/currency-format.html

Java: replace chars in String

/*
        Replaces all occurrences of given character with new one
        and returns new String object.
*/

String returnString = "test string to do some change";
returnString = returnString.replace( 'o', 'd' );

Source:
http://www.javadeveloper.co.in/java-example/java-string-replace-example.html
http://javarevisited.blogspot.com/2011/12/java-string-replace-example-tutorial.html
References:
http://download.oracle.com/javase/1.4.2/docs/api/java/lang/String.html

Java : Using currency format

// Using currency notations i.e. Colombian Currency
public String FormateoValor(Double valor){
NumberFormat n = NumberFormat.getCurrencyInstance(new Locale("es", "CO")); 
String returnString = n.format(valor.doubleValue());
return returnString ;  
}


References:
http://www.javaworld.com/javaworld/jw-06-2001/jw-0601-cents.html
http://www.herongyang.com/JDK/Locale-java-util-Local-Localization.html

Tuesday, September 6, 2011

Java: convert String to Long and vice versa

// String to long
long l = Long.parseLong(str);

// long to string
String numCadena= String.valueOf(l);

Sources:
http://www.java-tips.org/java-se-tips/java.lang/conversion-from-string-to-long.html
http://www.java-examples.com/java-string-valueof-example

Java: convert String to int and vice versa


// string to int
int numEntero = Integer.parseInt(numCadena);

// int to String
String numCadena= String.valueOf(numEntero);

Source:
http://emilio.aesinformatica.com/2007/11/22/pasar-de-int-a-string-y-de-string-a-int-en-java/

Sunday, September 4, 2011

Python : add days, hours, minutes to datetime

newDate = datetime.datetime.now() +timedelta(days=10,hours=23,minutes=15)

Reference:
http://www.daniweb.com/software-development/python/threads/192860

Excel 2007: Switch Columns and Rows



1. Open the spreadsheet you need to change.
2. If needed, insert a blank worksheet.
3. Click the first cell of your data range such as A1.
4. Shift-click the last cell of the range. Your selection should highlight.
5. From the Edit menu, select Copy.
6. At the bottom of the page, click the tab for the blank worksheet such as Sheet2.
7. In the blank worksheet, click cell A1.
8. From the Edit menu, select Paste Special. The Paste Special dialog should appear.
9. Click the checkbox for Transpose.
10. Click OK.


Source:
http://www.timeatlas.com/5_minute_tips/general/how_to_switch_excel_columns_and_rows