diff --git a/src/ui/Forms/JoinSubtitles.cs b/src/ui/Forms/JoinSubtitles.cs index 9035c0988..f78641c14 100644 --- a/src/ui/Forms/JoinSubtitles.cs +++ b/src/ui/Forms/JoinSubtitles.cs @@ -3,11 +3,11 @@ using Nikse.SubtitleEdit.Core.SubtitleFormats; using Nikse.SubtitleEdit.Logic; using System; using System.Collections.Generic; -using System.Globalization; using System.IO; using System.Linq; using System.Text; using System.Windows.Forms; +using Nikse.SubtitleEdit.Core.Enums; using MessageBox = Nikse.SubtitleEdit.Forms.SeMsgBox.MessageBox; namespace Nikse.SubtitleEdit.Forms @@ -94,13 +94,14 @@ namespace Nikse.SubtitleEdit.Forms private void listViewParts_DragDrop(object sender, DragEventArgs e) { var files = (string[])e.Data.GetData(DataFormats.FileDrop); - foreach (string fileName in files) + foreach (var fileName in files) { if (!_fileNamesToJoin.Any(file => file.Equals(fileName, StringComparison.OrdinalIgnoreCase))) { _fileNamesToJoin.Add(fileName); } } + SortAndLoad(); } @@ -266,21 +267,16 @@ namespace Nikse.SubtitleEdit.Forms if (!radioButtonJoinAddTime.Checked) { - for (int outer = 0; outer < subtitles.Count; outer++) + for (var outer = 0; outer < subtitles.Count; outer++) { - for (int inner = 1; inner < subtitles.Count; inner++) + for (var inner = 1; inner < subtitles.Count; inner++) { var a = subtitles[inner - 1]; var b = subtitles[inner]; if (a.Paragraphs.Count > 0 && b.Paragraphs.Count > 0 && a.Paragraphs[0].StartTime.TotalMilliseconds > b.Paragraphs[0].StartTime.TotalMilliseconds) { - var t1 = _fileNamesToJoin[inner - 1]; - _fileNamesToJoin[inner - 1] = _fileNamesToJoin[inner]; - _fileNamesToJoin[inner] = t1; - - var t2 = subtitles[inner - 1]; - subtitles[inner - 1] = subtitles[inner]; - subtitles[inner] = t2; + (_fileNamesToJoin[inner - 1], _fileNamesToJoin[inner]) = (_fileNamesToJoin[inner], _fileNamesToJoin[inner - 1]); + (subtitles[inner - 1], subtitles[inner]) = (subtitles[inner], subtitles[inner - 1]); } } } @@ -331,6 +327,11 @@ namespace Nikse.SubtitleEdit.Forms } } + if (radioButtonJoinPlain.Checked) + { + JoinedSubtitle.Sort(SubtitleSortCriteria.StartTime); + } + JoinedSubtitle.Renumber(); labelTotalLines.Text = string.Format(LanguageSettings.Current.JoinSubtitles.TotalNumberOfLinesX, JoinedSubtitle.Paragraphs.Count); } diff --git a/src/ui/Logic/ListViewSorter.cs b/src/ui/Logic/ListViewSorter.cs index c56b210f5..88c6de482 100644 --- a/src/ui/Logic/ListViewSorter.cs +++ b/src/ui/Logic/ListViewSorter.cs @@ -1,6 +1,8 @@ using Nikse.SubtitleEdit.Core.Common; using System; +using System.Collections.Generic; using System.Globalization; +using System.Linq; using System.Text.RegularExpressions; using System.Windows.Forms; @@ -13,6 +15,7 @@ namespace Nikse.SubtitleEdit.Logic public bool IsDisplayFileSize { get; set; } public bool Descending { get; set; } + private readonly Regex _numbers = new Regex(@"\d+", RegexOptions.Compiled); private readonly Regex _invariantNumber = new Regex(@"\d+\.{1,2}", RegexOptions.Compiled); public int Compare(object o1, object o2) @@ -32,7 +35,7 @@ namespace Nikse.SubtitleEdit.Logic var s1 = lvi1.SubItems[ColumnNumber].Text; var s2 = lvi2.SubItems[ColumnNumber].Text; - if (_invariantNumber.IsMatch(s1) && _invariantNumber.IsMatch(s2) && + if (_invariantNumber.IsMatch(s1) && _invariantNumber.IsMatch(s2) && int.TryParse(s1, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var i1) && int.TryParse(s2, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var i2)) { @@ -53,7 +56,10 @@ namespace Nikse.SubtitleEdit.Logic return i1 > i2 ? 1 : i1 == i2 ? 0 : -1; } - return string.Compare(lvi2.SubItems[ColumnNumber].Text, lvi1.SubItems[ColumnNumber].Text, StringComparison.Ordinal); + // use natural sort order + var str2 = _numbers.Replace(lvi2.SubItems[ColumnNumber].Text, m => m.Value.PadLeft(10, '0')).RemoveChar(' '); + var str1 = _numbers.Replace(lvi1.SubItems[ColumnNumber].Text, m => m.Value.PadLeft(10, '0')).RemoveChar(' '); + return string.Compare(str2, str1, StringComparison.Ordinal); } } }