/// <summary>
    /// This method is inherited from the IComparer interface.  It compares the two objects passed using a case insensitive comparison.
    /// </summary>
    /// <param name="x">First object to be compared</param>
    /// <param name="y">Second object to be compared</param>
    /// <returns>The result of the comparison. "0" if equal, negative if 'x' is less than 'y' and positive if 'x' is greater than 'y'</returns>
    public int Compare(object x, object y)
    {
        int          compareResult;
        ListViewItem listviewX, listviewY;

        // Cast the objects to be compared to ListViewItem objects
        listviewX = (ListViewItem)x;
        listviewY = (ListViewItem)y;

        // Compare the two items
        compareResult = ObjectCompare.Compare(listviewX.SubItems[ColumnToSort].Text, listviewY.SubItems[ColumnToSort].Text);

        // Calculate correct return value based on object comparison
        if (OrderOfSort == SortOrder.Ascending)
        {
            // Ascending sort is selected, return normal result of compare operation
            return(compareResult);
        }
        else if (OrderOfSort == SortOrder.Descending)
        {
            // Descending sort is selected, return negative result of compare operation
            return(-compareResult);
        }
        else
        {
            // Return '0' to indicate they are equal
            return(0);
        }
    }
 //protected void SortResults(List<WesternClassicalResult> results)
 //{
 //    //var results = list.Cast<WesternClassicalResult>() as List<WesternClassicalResult>;
 //    results.Sort((l, r) =>
 //    {
 //        return string.Compare(l.Composer.Name, r.Composer.Name, true);
 //    });
 //    results.ForEach((wcr) =>
 //    {
 //        if(wcr.Compositions != null)
 //        {
 //            wcr.Compositions.Sort((l, r) =>
 //            {
 //                return string.Compare(l.Composition.Name, r.Composition.Name, true);
 //            });
 //            wcr.Compositions.ForEach((cr) =>
 //            {
 //                if(cr.Performances != null)
 //                {
 //                    cr.Performances.Sort((l, r) =>
 //                    {
 //                        return string.Compare(l.Performance.Name, r.Performance.Name, true);
 //                    });
 //                }
 //            });
 //        }
 //    });
 //}
 protected void SortNaturalResults(List <WesternClassicalResult> results)
 {
     //var results = list.Cast<WesternClassicalResult>() as List<WesternClassicalResult>;
     results.Sort((l, r) =>
     {
         //var result = string.Compare(l.Composer.Name, r.Composer.Name, true);
         var result = l.Composer.Name.CompareIgnoreAccentsAndCase(r.Composer.Name);
         //Debug.WriteLine($"SortNaturalResults: left {l.Composer.Name}, right {r.Composer.Name}, right {result}");
         return(result);// string.Compare(l.Composer.Name, r.Composer.Name, true);
     });
     results.ForEach((wcr) =>
     {
         if (wcr.Compositions != null)
         {
             wcr.Compositions.Sort((l, r) => naturalComparer.Compare(l.Composition.Name, r.Composition.Name));
             wcr.Compositions.ForEach((cr) =>
             {
                 if (cr.Performances != null)
                 {
                     cr.Performances.Sort((l, r) =>
                     {
                         return(string.Compare(l.Performance.Name, r.Performance.Name, true));
                     });
                 }
             });
         }
     });
 }
        public int Compare(GameObject x, GameObject y)
        {
            if (x == null && y == null)
            {
                return(0);
            }
            else if (x == null)
            {
                return(-1);
            }
            else if (y == null)
            {
                return(1);
            }

            return(naturalStringComparer.Compare(x.name, y.name));
        }
        private void OnGUI()
        {
            if (!_isFirstDrawn && Event.current.type == EventType.Layout)
            {
                Rect rect = position;
                rect.width  = _windowSize.x;
                rect.height = _windowSize.y;
                position    = rect;

                _isFirstDrawn = true;
            }

            Texture2D[] inputTextures =
                Selection.objects
                .OfType <Texture2D>()
                .Where(tex => tex.GetTextureImporter().spriteImportMode == SpriteImportMode.None)
                .ToArray();

            GUILayout.Space(1f);
            EditorGUILayout.BeginHorizontal();
            {
                GUILayout.Label("Max atlas size: ", GUILayout.Width(90f));
                _atlasSize =
                    EditorGUILayout.IntPopup(
                        _atlasSize,
                        new[] { "256", "512", "1024", "2048", "4096" },
                        new[] { 256, 512, 1024, 2048, 4096 },
                        GUILayout.Width(50)
                        );

                GUILayout.Label("Padding (px): ", GUILayout.Width(80f));
                _atlasPadding = EditorGUILayout.IntSlider(_atlasPadding, 0, 10);
            }
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.BeginHorizontal(GUILayout.Width(200f));
            {
                EditorGUILayout.HelpBox(
                    inputTextures.Length > 0 ?
                    string.Format("Selected {0} texture(s)", inputTextures.Length) :
                    "Select textures in the Project view first.",
                    MessageType.Info,
                    true);
            }
            EditorGUILayout.EndHorizontal();

            GUI.enabled = inputTextures.Length > 0;
            if (GUILayout.Button("Pack into atlas"))
            {
                string atlasPath = EditorUtility.SaveFilePanelInProject("Save atlas image", "Atlas.png", "png", "Please enter a file name to save the atlas to");
                if (!string.IsNullOrEmpty(atlasPath))
                {
                    Texture2D       atlasTexture;
                    TextureImporter atlasTextureImported;

                    NaturalStringComparer naturalStringComparer = new NaturalStringComparer();
                    List <Texture2D>      sortedInputTextures   = inputTextures.ToList();
                    sortedInputTextures.Sort((tex1, tex2) => {
                        string path1 = AssetDatabase.GetAssetPath(tex1);
                        string path2 = AssetDatabase.GetAssetPath(tex2);

                        return(naturalStringComparer.Compare(path1, path2));
                    });
                    inputTextures = sortedInputTextures.ToArray();

                    PackTextures(inputTextures, atlasPath, _atlasSize, _atlasPadding, out atlasTexture, out atlasTextureImported);
                    EditorGUIUtility.PingObject(AssetDatabase.LoadAssetAtPath(atlasPath, typeof(Texture2D)));
                    Debug.Log(string.Format("Atlas image saved to \"{0}\"", atlasPath));
                }
            }
        }
 public int CompareStrings(string a, string b, bool ignoreWhiteSpace)
 {
     var comparer = new NaturalStringComparer(ignoreWhiteSpace);
     return comparer.Compare(a, b);
 }
        public void Compare()
        {
            var nsc = new NaturalStringComparer();

            Assert.AreEqual(0, nsc.Compare(null, null));
            Assert.AreEqual(-1, nsc.Compare(null, "X"));
            Assert.AreEqual(1, nsc.Compare("X", null));
            Assert.AreEqual(0, nsc.Compare("X", "X"));
            Assert.AreEqual(-1, nsc.Compare("X", "Y"));
            Assert.AreEqual(-1, nsc.Compare("2", "10"));
            Assert.AreEqual(-1, nsc.Compare("X2", "X10"));
            Assert.AreEqual(1, nsc.Compare("X2", "X1"));
            Assert.AreEqual(-1, nsc.Compare("v1.0.2", "v1.0.10"));
            Assert.AreEqual(-1, nsc.Compare("1.2", "1.10"));
            Assert.AreEqual(-1, nsc.Compare("1 2", "1 10"));
            Assert.AreEqual(-1, nsc.Compare("1.2", "1 10"));
            Assert.AreEqual(-1, nsc.Compare("1-2", "1/10"));
        }
        public void Compare()
        {
            var comparer = new NaturalStringComparer();

            Assert.That(comparer.Compare("abc", "abc"), Is.EqualTo(0));
            Assert.That(comparer.Compare("ABC", "abc"), Is.EqualTo(0));

            Assert.That(comparer.Compare("aac", "abc"), Is.LessThan(0));
            Assert.That(comparer.Compare("abc", "aac"), Is.GreaterThan(0));

            Assert.That(comparer.Compare("acc", "abc"), Is.GreaterThan(0));
            Assert.That(comparer.Compare("abc", "acc"), Is.LessThan(0));

            Assert.That(comparer.Compare("a01", "a10"), Is.LessThan(0));
            Assert.That(comparer.Compare("a10", "a01"), Is.GreaterThan(0));

            Assert.That(comparer.Compare("a1b", "a10b"), Is.LessThan(0));
            Assert.That(comparer.Compare("a10b", "a1b"), Is.GreaterThan(0));

            Assert.That(comparer.Compare("a١b", "a١٠b"), Is.LessThan(0));
            Assert.That(comparer.Compare("a١٠b", "a١b"), Is.GreaterThan(0));
        }
Exemple #8
0
 public int Compare(string x, string y)
 {
     return(NaturalStringComparer.Compare(x, y));
 }
Exemple #9
0
        private async Task CompareAsync(Archive archA, Archive archB, IProgress <int> progress, CancellationToken token)
        {
            var archAFileList = archA.Files.ToDictionary(x => x.FullPath.ToLower());
            var archBFileList = archB.Files.ToDictionary(x => x.FullPath.ToLower());

            // Merge file list, ignore duplicates
            var dict = archAFileList.Keys.ToDictionary(x => x);

            foreach (var file in archBFileList.Keys)
            {
                dict[file] = file;
            }
            var filelist = dict.Values.ToList();

            await Task.Run(() =>
            {
                using (var epA = archA.CreateSharedParams(true, false))
                    using (var epB = archB.CreateSharedParams(true, false))
                    {
                        int count          = 0;
                        int prevPercentage = 0;

                        this.Files.Clear();

                        foreach (var file in filelist)
                        {
                            token.ThrowIfCancellationRequested();

                            if (archAFileList.ContainsKey(file) && !archBFileList.ContainsKey(file))
                            {
                                // File appears in left archive only
                                this.Files.Add(new CompareItem(archAFileList[file].FullPath, CompareType.Removed));
                            }
                            else if (!archAFileList.ContainsKey(file) && archBFileList.ContainsKey(file))
                            {
                                // File appears in right archive only
                                this.Files.Add(new CompareItem(archBFileList[file].FullPath, CompareType.Added));
                            }
                            else
                            {
                                epA.Reader.BaseStream.Position = (long)archAFileList[file].Offset;
                                epB.Reader.BaseStream.Position = (long)archBFileList[file].Offset;

                                if (archAFileList[file].GetSizeInArchive(epA) == archBFileList[file].GetSizeInArchive(epB) &&
                                    CompareStreams(epA.Reader.BaseStream, epB.Reader.BaseStream, archAFileList[file].GetSizeInArchive(epA)))
                                {
                                    // Files are identical
                                    this.Files.Add(new CompareItem(archAFileList[file].FullPath, CompareType.Identical));
                                }
                                else
                                {
                                    // Files are different
                                    this.Files.Add(new CompareItem(archAFileList[file].FullPath, CompareType.Changed));
                                }
                            }

                            count++;
                            int newPercentage = (int)Math.Round((double)count / (double)filelist.Count * 100);
                            if (newPercentage != prevPercentage)
                            {
                                progress.Report(prevPercentage = newPercentage);
                            }
                        }
                    }

                this.Files.Sort((x, y) =>
                {
                    int comparison = ((int)x.Type).CompareTo((int)y.Type);
                    if (comparison != 0)
                    {
                        return(comparison);
                    }

                    return(NaturalStringComparer.Compare(x.FullPath, y.FullPath));
                });

                this.Filter();
            }, token);
        }