FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
RoyalLibrary/src/RoyalLibrary/SortingExtensions.cs at master · ByteDecoder/RoyalLibrary · GitHub
ByteDecoder
RoyalLibrary
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
RoyalLibrary
/
src
/
RoyalLibrary
/
SortingExtensions.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
90 lines (76 loc) · 3.09 KB
Breadcrumbs
RoyalLibrary
/
src
/
RoyalLibrary
/
SortingExtensions.cs
Copy path
File metadata and controls
90 lines (76 loc) · 3.09 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using
System
;
using
System
.
Collections
.
Generic
;
using
System
.
Linq
;
using
System
.
Threading
.
Tasks
;
using
ByteDecoder
.
Common
.
GuardClauses
;
namespace
ByteDecoder
.
RoyalLibrary
{
/// <summary>
/// Advanced sorting for collections
/// </summary>
public
static
class
SortingExtensions
{
/// <summary>
/// returns a new sorted ICollection
/// </summary>
/// <param name="source"></param>
/// <returns>An Enumerable sorted collection by LastName</returns>
public
static
async
Task
<
ICollection
<
string
>
>
SortByLastNameAsync
(
this
IEnumerable
<
string
>
source
)
{
Guard
.
Break
.
IfArgumentIsNull
(
source
,
nameof
(
source
)
)
;
var
enumerable
=
source
as
string
[
]
??
source
.
ToArray
(
)
;
var
rowsCount
=
enumerable
.
Count
(
)
;
if
(
rowsCount
==
0
)
return
(
ICollection
<
string
>
)
source
;
byte
lastSpaceIndex
;
string
firstName
,
lastName
;
var
orderedList
=
new
SortedList
<
string
,
string
>
(
)
{
Capacity
=
rowsCount
}
;
await
Task
.
Run
(
(
)
=>
{
foreach
(
var
item
in
enumerable
)
{
var
spaces
=
item
.
Count
(
char
.
IsWhiteSpace
)
;
lastSpaceIndex
=
(
byte
)
item
.
IndexOfNth
(
' '
,
spaces
-
1
)
;
firstName
=
item
.
Substring
(
0
,
lastSpaceIndex
)
;
var
lastNameLength
=
item
.
Length
-
firstName
.
Length
;
lastName
=
item
.
Substring
(
lastSpaceIndex
,
lastNameLength
)
;
orderedList
.
Add
(
lastName
,
firstName
)
;
}
}
)
;
return
orderedList
.
AsParallel
(
)
.
AsOrdered
(
)
.
Select
(
(
fullName
)
=>
fullName
.
Value
+
fullName
.
Key
)
.
ToList
(
)
;
}
/// <summary>
/// returns a new sorted ICollection
/// </summary>
/// <param name="source"></param>
/// <param name="action"></param>
/// <returns>An Enumerable sorted collection by LastName</returns>
public
static
async
Task
<
ICollection
<
string
>
>
SortByLastNameAsync
(
this
IEnumerable
<
string
>
source
,
Action
<
string
>
action
)
{
var
sortedList
=
await
source
.
SortByLastNameAsync
(
)
;
sortedList
.
AsParallel
(
)
.
AsOrdered
(
)
.
ForEach
(
action
)
;
return
sortedList
;
}
/// <summary>
///
/// </summary>
/// <param name="str"></param>
/// <param name="value"></param>
/// <param name="nth"></param>
/// <returns></returns>
private
static
int
IndexOfNth
(
this
string
str
,
char
value
,
int
nth
)
{
if
(
nth
<
0
)
throw
new
ArgumentException
(
$
"Negative index found
{
nameof
(
nth
)
}
has negative value, it must start at 0"
)
;
var
offset
=
str
.
IndexOf
(
value
)
;
for
(
var
i
=
0
;
i
<
nth
;
i
++
)
{
if
(
offset
==
-
1
)
return
-
1
;
offset
=
str
.
IndexOf
(
value
,
offset
+
1
)
;
}
return
offset
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL