Skip to content

Commit 6c9cd50

Browse files
authored
Merge pull request #15 from Exarchiasghost/master
Updated searching and sorting README.md
2 parents 1278b01 + 9d9c906 commit 6c9cd50

File tree

2 files changed

+52
-11
lines changed

2 files changed

+52
-11
lines changed

Data_Structures/Arrays/Searching/README.md

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,28 @@
11
![](http://i.imgur.com/BgUMUGU.png)
22

33
# Searching
4-
This MD file serves as example/template. It will be filled up soon.
5-
6-
(for Bucky to fill up)
4+
In order to iterate an array more efficiently sometimes we have to search for the value instead of the place. The function **indexof()** helps us with that
75

86
# Course Documentation
97

10-
(this for me to fill up with every element that you use in lessons. syntax explaination and links for more)
118

12-
## Element to explain
9+
## .indexof()
10+
In our example:
11+
12+
13+
let items = [40, 68, 58, 12, 80, 37, 13, 63, 42, 3, 58];
14+
console.log(items.indexOf(12)); // 3
15+
console.log(items.indexOf(99)); // -1 (not found)
16+
console.log(items.indexOf(58)); // 2
17+
console.log(items.lastIndexOf(58)); // 10
18+
19+
20+
it is pretty forward that we use the function **.indexOF()** to find a specific value that is stored in our array.
21+
For finding the value "a" in the array "anarray" we are using the following syntax:
22+
23+
anarray.indexOF("a")
1324

14-
(for example console.log)
25+
1526

1627
##Javascript functions
1728

@@ -125,4 +136,5 @@ stack in order to remove things from it.
125136
- [Twitter](https://twitter.com/bucky_roberts)
126137
- [Google+](https://plus.google.com/+BuckyRoberts)
127138
- [reddit](https://www.reddit.com/r/thenewboston/)
139+
128140
> Written with [StackEdit](https://stackedit.io/).

Data_Structures/Arrays/Sorting/README.md

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,44 @@
11
![](http://i.imgur.com/BgUMUGU.png)
22

33
# Sorting
4-
This MD file serves as example/template. It will be filled up soon.
4+
Sometimes we need to sort the stored values of our array. In order to do that we usually use the .sort() function:
5+
56

6-
(for Bucky to fill up)
7+
console.log(items.sort());
78

89
# Course Documentation
910

10-
(this for me to fill up with every element that you use in lessons. syntax explaination and links for more)
11+
## .sort()
12+
13+
According to [w3schools](http://www.w3schools.com/jsref/jsref_sort.asp):
14+
15+
"The sort() method sorts the items of an array.
16+
The sort order can be either alphabetic or numeric, and either ascending (up) or descending (down).
17+
By default, the sort() method sorts the values as strings in alphabetical and ascending order.
18+
This works well for strings ("Apple" comes before "Banana"). **However, if numbers are sorted as strings, "25" is bigger than "100", because "2" is bigger than "1".**
19+
Because of this, the sort() method will produce an incorrect result when sorting numbers.
20+
You can fix this by providing a "compare function" (See "Parameter Values" below)."
21+
22+
let items = [1, 10, 17, 18, 2, 7, 3, 19, 14, 5];
23+
console.log(items.reverse());
24+
25+
// not what we wanted, because it sorts the numbers as if they are all strings
26+
console.log(items.sort());
27+
28+
###compareFunction
1129

12-
## Element to explain
30+
Optional. A function that defines an alternative sort order. The function should return a negative, zero, or positive value, depending on the arguments, like:
1331

14-
(for example console.log)
32+
function(a, b){return a-b}
33+
34+
When the sort() method compares two values, it sends the values to the compare function, and sorts the values according to the returned (negative, zero, positive) value.
35+
36+
items.sort(function (a, b) {
37+
return a - b;
38+
});
39+
console.log(items);
40+
41+
[Read more about sort() function on W3schools](http://www.w3schools.com/jsref/jsref_sort.asp).
1542

1643
##Javascript functions
1744

@@ -124,4 +151,6 @@ stack in order to remove things from it.
124151
- [Twitter](https://twitter.com/bucky_roberts)
125152
- [Google+](https://plus.google.com/+BuckyRoberts)
126153
- [reddit](https://www.reddit.com/r/thenewboston/)
154+
155+
127156
> Written with [StackEdit](https://stackedit.io/).

0 commit comments

Comments
 (0)