473,382 Members | 1,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

indexing a list by id

Hi.

I have a list. List should be indexed with id number, but how i can do
it? Should I get list id's to array and use sort or is there better
solutions for that?

<ul id='List'>
<li id="list_3">Apples</li>
<li id="list_1">Grapes</li>
<li id="list_2">Strawberries</li>
</ul>
<a href="#" onclick="sList()">Sortable</a>
<script>
function sList(){

}

</script>
Aug 6 '08 #1
2 1335
On Aug 6, 6:36*am, torso <petteri.torsso...@gmail.comwrote:
Hi.

I have a list. List should be indexed with id number, but how i can do
it? Should I get list id's to array and use sort or is there better
solutions for that?

*<ul id='List'>
* * * * * * * * * * * * * * * * <li id="list_3">Apples</li>
* * * * * * * * * * * * * * * * <li id="list_1">Grapes</li>
* * * * * * * * * * * * * * * * <li id="list_2">Strawberries</li>
*</ul>
<a href="#" onclick="sList()">Sortable</a>
<script>
function sList(){

}

</script>
I can't say this is the best way but something like this might get you
started:

<script type="text/javascript">
function sortList(id) {
var ul = document.getElementById(id);
var lis = new Array();
while (ul.getElementsByTagName('LI').length 0) {
var li = ul.getElementsByTagName('LI')[0];
lis.push(li);
ul.removeChild(li);
}
lis.sort(function(a, b) {
if (a.id < b.id) {
return -1;
}
else if (a.id b.id) {
return 1;
}
else {
return 0;
}
});
for (var i = 0; i < lis.length; i++) {
ul.appendChild(lis[i]);
}
}
</script>

...

<ul id="mylist">
<li id="item3">Item3</li>
<li id="item1">Item1</li>
<li id="item2">Item2</li>
</ul>
<input type="button" value="Sort" onclick="sortList('mylist');"/>

Basically just created an array of li elements and removed them from
the ul element, called the sort method on the array using a function
that compared ids, and put the li elements back in the ul element.

You could even allow them to sort in both orders (first up, then down)
via a simple change in the javascript:

<script type="text/javascript">
var reverse = new Array();

function sortList(id) {
var ul = document.getElementById(id);
var lis = new Array();
while (ul.getElementsByTagName('LI').length 0) {
var li = ul.getElementsByTagName('LI')[0];
lis.push(li);
ul.removeChild(li);
}
lis.sort(function(a, b) {
if (a.id < b.id) {
return -1;
}
else if (a.id b.id) {
return 1;
}
else {
return 0;
}
});
if (reverse[id] == true) {
lis.reverse();
}
reverse[id] = ! reverse[id];
for (var i = 0; i < lis.length; i++) {
ul.appendChild(lis[i]);
}
}
</script>

HTH.
Aug 6 '08 #2
On 6 elo, 15:11, Tom Cole <tco...@gmail.comwrote:
On Aug 6, 6:36*am, torso <petteri.torsso...@gmail.comwrote:
Hi.
I have a list. List should be indexed with id number, but how i can do
it? Should I get list id's to array and use sort or is there better
solutions for that?
*<ul id='List'>
* * * * * * * * * * * * * * * * <li id="list_3">Apples</li>
* * * * * * * * * * * * * * * * <li id="list_1">Grapes</li>
* * * * * * * * * * * * * * * * <li id="list_2">Strawberries</li>
*</ul>
<a href="#" onclick="sList()">Sortable</a>
<script>
function sList(){
}
</script>

I can't say this is the best way but something like this might get you
started:

<script type="text/javascript">
* * * * * function sortList(id) {
* * * * * * * * var ul = document.getElementById(id);
* * * * * * * * var lis = new Array();
* * * * * * * * while (ul.getElementsByTagName('LI').length 0) {
* * * * * * * * * * * * var li = ul.getElementsByTagName('LI')[0];
* * * * * * * * * * * * lis.push(li);
* * * * * * * * * * * * ul.removeChild(li);
* * * * * * * * }
* * * * * * * * lis.sort(function(a, b) {
* * * * * * * * * * * * if (a.id < b.id) {
* * * * * * * * * * * * * * * * return -1;
* * * * * * * * * * * * }
* * * * * * * * * * * * else if (a.id b.id) {
* * * * * * * * * * * * * * * * return 1;
* * * * * * * * * * * * }
* * * * * * * * * * * * else {
* * * * * * * * * * * * * * * * return 0;
* * * * * * * * * * * * }
* * * * * * * * });
* * * * * * * * for (var i = 0; i < lis.length; i++) {
* * * * * * * * * * * * ul.appendChild(lis[i]);
* * * * * * * * }
* * * * }
</script>

...

<ul id="mylist">
* * <li id="item3">Item3</li>
* * <li id="item1">Item1</li>
* * <li id="item2">Item2</li>
</ul>
<input type="button" value="Sort" onclick="sortList('mylist');"/>

Basically just created an array of li elements and removed them from
the ul element, called the sort method on the array using a function
that compared ids, and put the li elements back in the ul element.

You could even allow them to sort in both orders (first up, then down)
via a simple change in the javascript:

<script type="text/javascript">
* * * * * var reverse = new Array();

* * * * * function sortList(id) {
* * * * * * * * var ul = document.getElementById(id);
* * * * * * * * var lis = new Array();
* * * * * * * * while (ul.getElementsByTagName('LI').length 0) {
* * * * * * * * * * * * var li = ul.getElementsByTagName('LI')[0];
* * * * * * * * * * * * lis.push(li);
* * * * * * * * * * * * ul.removeChild(li);
* * * * * * * * }
* * * * * * * * lis.sort(function(a, b) {
* * * * * * * * * * * * if (a.id < b.id) {
* * * * * * * * * * * * * * * * return -1;
* * * * * * * * * * * * }
* * * * * * * * * * * * else if (a.id b.id) {
* * * * * * * * * * * * * * * * return 1;
* * * * * * * * * * * * }
* * * * * * * * * * * * else {
* * * * * * * * * * * * * * * * return 0;
* * * * * * * * * * * * }
* * * * * * * * });
* * * * * * * * * if (reverse[id] == true) {
* * * * * * * * * * * * * * lis.reverse();
* * * * * * * * * }
* * * * * * * * * reverse[id] = ! reverse[id];
* * * * * * * * for (var i = 0; i < lis.length; i++) {
* * * * * * * * * * * * ul.appendChild(lis[i]);
* * * * * * * * }
* * * * }
</script>

HTH.
Thanks for help.
Aug 6 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Hilde Roth | last post by:
This may have been asked before but I can't find it. If I have a rectangular list of lists, say, l = ,,], is there a handy syntax for retrieving the ith item of every sublist? I know about for i...
6
by: Michael Drumheller | last post by:
(If you're not interested in NumArray, please skip this message.) I am new to NumArray and I wonder if someone can help me with array-indexing. Here's the basic situation: Given a rank-2 array...
12
by: Steven Bethard | last post by:
So I need to do something like: for i in range(len(l)): for j in range(i+1, len(l)): # do something with (l, l) where I get all pairs of items in a list (where I'm thinking of pairs as sets,...
10
by: Ishwor | last post by:
Hi all. Look at this snippet of code. >>> l = >>> l >>> l 'a' It prints the value 'a'. Fine so far :-) l ---> 'a' . l---> 'a' --> 'a'.
108
by: Bryan Olson | last post by:
The Python slice type has one method 'indices', and reportedly: This method takes a single integer argument /length/ and computes information about the extended slice that the slice object would...
3
by: Chung Leong | last post by:
Here's the rest of the tutorial I started earlier: Aside from text within a document, Indexing Service let you search on meta information stored in the files. For example, MusicArtist and...
6
by: Derek Peschel | last post by:
Here are two functions. def invert_dict_to_lists(dict): lists = {} for key in dict: value = dict if not value in lists: lists = else: lists.append(key)
4
by: Emin | last post by:
Dear Experts, How much slower is dict indexing vs. list indexing (or indexing into a numpy array)? I realize that looking up a value in a dict should be constant time, but does anyone have a...
6
by: 78ncp | last post by:
hi... how to implementation algorithm latent semantic indexing in python programming...?? thank's for daniel who answered my question before.. -- View this message in context:...
3
by: Rüdiger Werner | last post by:
Hello! Out of curiosity and to learn a little bit about the numpy package i've tryed to implement a vectorised version of the 'Sieve of Zakiya'. While the code itself works fine it is...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.